为什么使用AFNetworking而不是dispatch_async? [英] Why use AFNetworking instead of dispatch_async?

查看:80
本文介绍了为什么使用AFNetworking而不是dispatch_async?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要使用AFNetworking的异步方法,只需使用GCD即可完成异步调用?

Why should one use AFNetworking's async methods, when an async call can be done simply with GCD?

dispatch_async(bgQ, ^{

                //NSURLConnection code

                dispatch_async(dispatch_get_main_queue(), ^{
                    //UI code
                });
});


推荐答案

这在<#的顶部详细解答a href =https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ> AFNetworking常见问题解答:


NSURLConnection 提供 + sendAsynchronousRequest:queue:completionHandler: + sendSynchronousRequest:returningResponse:错误:,使用AFNetworking有很多好处:

While NSURLConnection provides +sendAsynchronousRequest:queue:completionHandler: and +sendSynchronousRequest:returningResponse:error:, there are many benefits to using AFNetworking:


  • AFURLConnectionOperation 及其子类继承自 NSOperation ,它允许取消,暂停/恢复请求,并通过 NSOperationQueue

  • AFURLConnectionOperation 还允许您轻松地上传和下载流,处理身份验证挑战,>监控上传和下载进度,并控制缓存行为或请求。

  • AFHTTPRequestOperation 和它的子类基于HTTP>状态代码和内容类型区分成功和不成功的请求。

  • AFNetworking包括转换 NSData 更多可用的格式,如JSON,> XML,图像和属性列表。

  • AFHTTPClient 提供了一个方便的交互界面使用Web服务,包括默认标头,身份验证,>网络可访问性监控,批量操作,查询字符串参数序列化和多部分表单请求。

  • UIImageView + 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.

这篇关于为什么使用AFNetworking而不是dispatch_async?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆