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' –
它事實證明我錯了,這並沒有在主線上運行,但非常感謝您的幫助! –