用于 JSON 请求的异步 CompletionHandler

在使用了 AlamoFire 框架之后,我注意到 CompletionHandler 是在主线程上运行的。我想知道下面的代码是否是在完成处理程序中创建核心数据导入任务的良好实践:

Alamofire.request(.GET, "http://myWebSite.example", parameters: parameters)
.responseJSON(options: .MutableContainers) { (_, _, JSON, error) -> Void in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
if let err = error{
println("Error:\(error)")
return;
}


if let jsonArray = JSON as? [NSArray]{
let importer = CDImporter(incomingArray: jsonArray entity: "Artist", map: artistEntityMap);


}
});
}
38678 次浏览

这真是个好问题。你的方法非常有效。然而,Alamofire 实际上可以帮助您更加简化这个过程。

示例代码分派队列故障

在您的示例代码中,您在以下调度队列之间跳转:

  1. 调度队列
  2. 用于验证和序列化程序处理的任务委托分派队列
  3. 调用完成处理程序的主分派队列
  4. JSON 处理的高优先级队列
  5. 更新用户界面的主调度队列(如果需要)

如你所见,你到处乱蹦。让我们看看另一种利用 Alamofire 内部强大功能的方法。

Alamofire 响应调度队列

Alamofire 有一个内置在它自己的低级处理中的最佳方法。如果您选择使用自定义分派队列,那么最终由所有自定义响应序列化器调用的单个 response方法就支持自定义分派队列。

虽然 GCD 在分派队列之间跳转方面非常出色,但是您需要避免跳转到繁忙的队列(例如主线程)。通过消除在异步处理中间跳回到主线程的操作,您可能会大大加快速度。下面的示例演示如何使用 Alamofire 逻辑直接开箱即用完成此任务。

Alamofire 1. x

let queue = dispatch_queue_create("com.cnoon.manager-response-queue", DISPATCH_QUEUE_CONCURRENT)


let request = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
request.response(
queue: queue,
serializer: Request.JSONResponseSerializer(options: .AllowFragments),
completionHandler: { _, _, JSON, _ in


// You are now running on the concurrent `queue` you created earlier.
println("Parsing JSON on thread: \(NSThread.currentThread()) is main thread: \(NSThread.isMainThread())")


// Validate your JSON response and convert into model objects if necessary
println(JSON)


// To update anything on the main thread, just jump back on like so.
dispatch_async(dispatch_get_main_queue()) {
println("Am I back on the main thread: \(NSThread.isMainThread())")
}
}
)

Alamofire 3.x (Swift 2.2和2.3)

let queue = dispatch_queue_create("com.cnoon.manager-response-queue", DISPATCH_QUEUE_CONCURRENT)


let request = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
request.response(
queue: queue,
responseSerializer: Request.JSONResponseSerializer(options: .AllowFragments),
completionHandler: { response in
// You are now running on the concurrent `queue` you created earlier.
print("Parsing JSON on thread: \(NSThread.currentThread()) is main thread: \(NSThread.isMainThread())")


// Validate your JSON response and convert into model objects if necessary
print(response.result.value)


// To update anything on the main thread, just jump back on like so.
dispatch_async(dispatch_get_main_queue()) {
print("Am I back on the main thread: \(NSThread.isMainThread())")
}
}
)

Alamofire 4.x (Swift 3)

let queue = DispatchQueue(label: "com.cnoon.response-queue", qos: .utility, attributes: [.concurrent])


Alamofire.request("http://httpbin.org/get", parameters: ["foo": "bar"])
.response(
queue: queue,
responseSerializer: DataRequest.jsonResponseSerializer(),
completionHandler: { response in
// You are now running on the concurrent `queue` you created earlier.
print("Parsing JSON on thread: \(Thread.current) is main thread: \(Thread.isMainThread)")


// Validate your JSON response and convert into model objects if necessary
print(response.result.value)


// To update anything on the main thread, just jump back on like so.
DispatchQueue.main.async {
print("Am I back on the main thread: \(Thread.isMainThread)")
}
}
)

Alamofire 调度队列故障

下面是此方法所涉及的不同分派队列的分解。

  1. 调度队列
  2. 用于验证和序列化程序处理的任务委托分派队列
  3. 用于 JSON 处理的自定义管理器并发调度队列
  4. 更新用户界面的主调度队列(如果需要)

摘要

通过消除返回主分派队列的第一个跃点,您消除了潜在的瓶颈,并且使整个请求和处理都是异步的。太棒了!

说到这里,我再怎么强调熟悉 Alamofire 的内部工作原理都不为过。您永远不知道什么时候可以找到真正能帮助您改进自己的代码的东西。

如果你像我一样使用 ResponseObjectSerializable,你可以把这个并发行为嵌入到请求扩展本身:

extension Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T, NSError> -> Void) -> Self {
let responseSerializer = ResponseSerializer<T, NSError> { request, response, data, error in
guard error == nil else { return .Failure(error!) }


let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)


switch result {
case .Success(let value):
if let
response = response,
responseObject = T(response: response, representation: value)
{
return .Success(responseObject)
} else {
let failureReason = "JSON could not be serialized into response object: \(value)"
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
case .Failure(let error):
return .Failure(error)
}
}


let queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT)
return response(queue: queue, responseSerializer: responseSerializer) { response in
dispatch_async(dispatch_get_main_queue()) {
completionHandler(response)
}
}
}
}

Swift 3.0的小更新,Alamofire (4.0.1) ,编辑@cnon 回答:

let queue = DispatchQueue(label: "com.cnoon.manager-response-queue",
qos: .userInitiated,
attributes:.concurrent)
Alamofire?.request(SERVER_URL, method: .post,
parameters: ["foo": "bar"],
encoding: JSONEncoding.default,//by default
headers: ["Content-Type":"application/json; charset=UTF-8"])
.validate(statusCode: 200..<300).//by default
responseJSON(queue: queue, options: .allowFragments,
completionHandler: { (response:DataResponse<Any>) in


switch(response.result) {
case .success(_):
break
case .failure(_):
print(response.result.error)
if response.result.error?._code == NSURLErrorTimedOut{
//TODO: Show Alert view on netwok connection.
}
break
}
})