在 Swift 中使用 Alamofire 频繁地从 API 拉入
Pulling in frequent times from API with Alamofire in Swift
我是 Swift 的新手,我想每 5 秒使用 get 请求从 API 中提取,我已经完成了 HTTP 请求提取和解析。我只是想知道我应该在控制器中的什么地方放置某种循环 class.
这是我的控制器class代码:
import UIKit
import Alamofire
import SwiftyJSON
class HomePageViewController: UIViewController {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var age: UILabel!
@IBOutlet weak var email: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getRequest()
// Do any additional setup after loading the view.
}
// the function that get Request from the API
func getRequest(){
AF.request("http://vocation.cs.umd.edu/flask/tempjson", method: .get)
.responseJSON { (response) in
do{
let json = try JSON(data: response.data!)
let nm = json["username"].string
let ag = json["age"].int
let em = json["email"].string
// display
self.name.text = "Welcome, \(nm ?? "")"
self.age.text = "Age: \(ag ?? 0)"
self.email.text = "Email: \(em ?? "")"
} catch let jsonErr{
print(jsonErr)
}
}
}
}
我知道我把拉取功能放在了viewDidLoad()
,但是在这个页面以5s的频率停留在什么位置执行这个功能,用什么样的循环才能保证是每 5 秒做一次吗?谢谢!!
如果您只想每 5 秒打一次电话,您可以使用预定的计时器
class HomePageViewController: UIViewController {
// ...
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { [weak self] (timer) in
self?.getRequest()
})
getRequest() // Call it initially because the scheduledTimer will only call it after the initial 5 seconds
}
func getRequest() {
// ...
}
}
我建议等待请求的响应(如果互联网不好,请求可能会超过 5 秒,最终会出现“重叠”请求。
这里是响应5秒后执行请求的解决方案
class HomePageViewController: UIViewController {
// ...
override func viewDidLoad() {
super.viewDidLoad()
getRequest()
}
func getRequest() {
AF.request("http://vocation.cs.umd.edu/flask/tempjson", method: .get).responseJSON { (response) in
do{
let json = try JSON(data: response.data!)
let nm = json["username"].string
let ag = json["age"].int
let em = json["email"].string
// display
self.name.text = "Welcome, \(nm ?? "")"
self.age.text = "Age: \(ag ?? 0)"
self.email.text = "Email: \(em ?? "")"
} catch let jsonErr{
print(jsonErr)
}
// Trigger a new request 5s after the response
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: { [weak self] in
self?.getRequest()
})
}
}
}
我是 Swift 的新手,我想每 5 秒使用 get 请求从 API 中提取,我已经完成了 HTTP 请求提取和解析。我只是想知道我应该在控制器中的什么地方放置某种循环 class.
这是我的控制器class代码:
import UIKit
import Alamofire
import SwiftyJSON
class HomePageViewController: UIViewController {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var age: UILabel!
@IBOutlet weak var email: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getRequest()
// Do any additional setup after loading the view.
}
// the function that get Request from the API
func getRequest(){
AF.request("http://vocation.cs.umd.edu/flask/tempjson", method: .get)
.responseJSON { (response) in
do{
let json = try JSON(data: response.data!)
let nm = json["username"].string
let ag = json["age"].int
let em = json["email"].string
// display
self.name.text = "Welcome, \(nm ?? "")"
self.age.text = "Age: \(ag ?? 0)"
self.email.text = "Email: \(em ?? "")"
} catch let jsonErr{
print(jsonErr)
}
}
}
}
我知道我把拉取功能放在了viewDidLoad()
,但是在这个页面以5s的频率停留在什么位置执行这个功能,用什么样的循环才能保证是每 5 秒做一次吗?谢谢!!
如果您只想每 5 秒打一次电话,您可以使用预定的计时器
class HomePageViewController: UIViewController {
// ...
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { [weak self] (timer) in
self?.getRequest()
})
getRequest() // Call it initially because the scheduledTimer will only call it after the initial 5 seconds
}
func getRequest() {
// ...
}
}
我建议等待请求的响应(如果互联网不好,请求可能会超过 5 秒,最终会出现“重叠”请求。
这里是响应5秒后执行请求的解决方案
class HomePageViewController: UIViewController {
// ...
override func viewDidLoad() {
super.viewDidLoad()
getRequest()
}
func getRequest() {
AF.request("http://vocation.cs.umd.edu/flask/tempjson", method: .get).responseJSON { (response) in
do{
let json = try JSON(data: response.data!)
let nm = json["username"].string
let ag = json["age"].int
let em = json["email"].string
// display
self.name.text = "Welcome, \(nm ?? "")"
self.age.text = "Age: \(ag ?? 0)"
self.email.text = "Email: \(em ?? "")"
} catch let jsonErr{
print(jsonErr)
}
// Trigger a new request 5s after the response
DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: { [weak self] in
self?.getRequest()
})
}
}
}