将 GCD 与也进行异步调用的代码一起使用是否安全
Is it safe to use GCD with code that also makes asynchronous calls
我有一个应用程序可以为 HTTP 请求执行一些复杂的构建数据工作,目前请求本身(使用 AFNetworking)也是将由 dispatch_async
包装的方法的一部分。重构事物以便不包装 AFNetworking 调用是否更好?
// expensive set up code then...
AFHTTPRequestOperation *requestOperation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response %@", responseObject);
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@", error);
failure(error);
}];
requestOperation.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
[[NSOperationQueue mainQueue] addOperation:requestOperation];
return requestOperation;
在哪个线程上调用它并不重要。 AFNetworking 将在自己的队列上创建请求操作,并在主队列上调用完成处理程序。
我有一个应用程序可以为 HTTP 请求执行一些复杂的构建数据工作,目前请求本身(使用 AFNetworking)也是将由 dispatch_async
包装的方法的一部分。重构事物以便不包装 AFNetworking 调用是否更好?
// expensive set up code then...
AFHTTPRequestOperation *requestOperation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response %@", responseObject);
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@", error);
failure(error);
}];
requestOperation.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
[[NSOperationQueue mainQueue] addOperation:requestOperation];
return requestOperation;
在哪个线程上调用它并不重要。 AFNetworking 将在自己的队列上创建请求操作,并在主队列上调用完成处理程序。