objective-c - 为什么使用 AFNetworking 而不是 dispatch_async?

标签 objective-c ios networking afnetworking

当异步调用可以简单地使用 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 from NSOperation, which allows requests to be cancelled, > suspended / resumed, and managed by an NSOperationQueue.
  • 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/

相关文章:

ios - 将CALayer与位置一起移动,而不是与值之间移动?

ios - Swift下载速度说明

c - 本地网络外的 UDP "sendto"

ios - 如何使用 IBAction 清除 UITextField 中的文本?

iphone - 重复的 UIView

objective-c - iOS 中的属性和综合

sql - Azure - 从我的本地计算机连接到 SQL Server

ios - UIScrollView - 需要 x 位置/宽度的约束,需要 y 位置/高度的约束

iphone - 延迟后不使用 NSThread 或 sleep() 执行方法 - 还有其他选择吗?

ios - UITableView 未填满屏幕底部