我需要将我的 Alamofire 调用包装在 dispatch_async 内吗?
Do I need to wrap my Alamofire calls inside dispatch_async?
func authenticate(completion:(success: Bool) -> Void) {
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos, 0)){ () -> Void in
Alamofire.request(.POST, CONSTANTS.Domain+"/accounts", parameters: ["" : ""]).responseJSON { (req, res, json, error) in
dispatch_async(dispatch_get_main_queue()){
completion(success: true)
}
}
}
}
或者,我可以省略调度并保持代码简单吗?
Alamofire is designed to be asynchronous. 另一方面,如果方法有回调,很可能是异步的。
所以,是的,您可以省略 dispatch_async 调用。
func authenticate(completion:(success: Bool) -> Void) {
let qos = Int(QOS_CLASS_USER_INITIATED.value)
dispatch_async(dispatch_get_global_queue(qos, 0)){ () -> Void in
Alamofire.request(.POST, CONSTANTS.Domain+"/accounts", parameters: ["" : ""]).responseJSON { (req, res, json, error) in
dispatch_async(dispatch_get_main_queue()){
completion(success: true)
}
}
}
}
或者,我可以省略调度并保持代码简单吗?
Alamofire is designed to be asynchronous. 另一方面,如果方法有回调,很可能是异步的。 所以,是的,您可以省略 dispatch_async 调用。