当异步调用可以简单地使用 GCD 完成时,为什么要使用 AFNetworking 的异步方法?
dispatch_async(bgQ, ^{
//NSURLConnection code
dispatch_async(dispatch_get_main_queue(), ^{
//UI code
});
});
最佳答案
这在 AFNetworking FAQ 的顶部有详细的回答。 :
While
NSURLConnection
provides+sendAsynchronousRequest:queue:completionHandler:
and+sendSynchronousRequest:returningResponse:error:
, there are many benefits to using AFNetworking:
AFURLConnectionOperation
and its subclasses inherit fromNSOperation
, which allows requests to be cancelled, > suspended / resumed, and managed by anNSOperationQueue
.AFURLConnectionOperation
also allows you to easily stream uploads and downloads, handle authentication challenges, > monitor upload and download progress, and control the caching behavior or requests.AFHTTPRequestOperation
and its subclasses distinguish between successful and unsuccessful requests based on HTTP > status codes and content type.- AFNetworking includes media-specific request operations that transform
NSData
into more useable formats, like JSON, > XML, images, and property lists.AFHTTPClient
provides a convenient interface to interact with web services, including default headers, authentication, > network reachability monitoring, batched operations, query string parameter serialization, and multipart form requests.UIImageView+AFNetworking
adds a convenient way to asynchronously loads images.
关于objective-c - 为什么使用 AFNetworking 而不是 dispatch_async?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133591/