0
我们正在使用Alamofire进行API调用,并希望它是异步的,但延迟2秒。我们正在使用这个来延迟通话,但我认为这可能会导致在主线程上运行某些内容。等待2秒钟来运行这样的请求的最佳方式是什么?延迟Alamofire请求
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
//This is the Alamofire API call
self.excecuteGetRequest(id, product: product)
}
这是我尝试过的其他东西,但我不确定它是否正确。
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let qualityOfServiceClass = DispatchQoS.QoSClass.userInitiated
let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgroundQueue.async(execute: {
self.excecuteGetRequest(cartID, product: product)
})
}
这里是请求是否有帮助:
func excecuteGetRequest(_ id:String,product:Product) {
let url = "www.api.sampleAPI.com/random"
Alamofire.request(url).responseJSON(completionHandler: { (innerResponse) in
print("Inner response = \(innerResponse)")
}
}
不要采用主队列,请尝试使用'DispatchQueue.global' –
它事实证明我错了,这并没有在主线上运行,但非常感谢您的帮助! –