[翻译] Working with NSURLSession: AFNetworking 2.0

简介:

Working with NSURLSession: AFNetworking 2.0

 
简单翻译,有很多错误,看官无法理解处请英文原文对照.
http://code.tutsplus.com/tutorials/working-with-nsurlsession-afnetworking-20--mobile-22651
by  Bart Jacobs3 Feb 2014

In the previous installments of this series, we've taken a close look at the NSURLSession API introduced in iOS 7 and OS X Mavericks. Networking on iOS and OS X has become much simpler and more flexible thanks to the NSURLSession API. Does this mean that you should stop using AFNetworking for your networking needs? And what about AFNetworking 2.0, which was introduced a few months ago? In this final installment, I will tell you about AFNetworking 2.0 and how it compares to the NSURLSession API.

在之前的一系列教程当中,我们已经仔细的分析了 NSURLSession 的 API,他是 iOS 7 以及 OS X 新引入的处理网络链接的方法。如今,处理网络越来越简单,小白都能轻易上手了,这得益于 NSURLSession 的帮助哦,亲。虽然小白都能整明白网络是怎么回事,是否意味着不需要使用开源库 AFNetworking 了呢? AFNetworking 2.0 到底有着怎样的特性呢?在这最后的一期教程当中,哥来引领你们,进行 AFNetworking 2.0 与 NSURLSession 之间的纵深比较。

Introduction (介绍

By all accounts, AFNetworking is about as mainstream as it gets.

As Mattt Thompson points out on NSHipster, AFNetworking has become the de facto networking library for Cocoa applications. At the time of writing, the library has surpassed 10,000 stars on GitHub and was forked close to 3,000 times.

If you're new to AFNetworking and would like to read more about its history and features, then I suggest to read Mattt's post aboutAFNetworking 2.0 on NSHipster. In this article, I'd like to focus on two aspects, what is new in AFNetworking 2.0 and how does it compare to the NSURLSession API . The question you should be asking yourself is "Should I still be using AFNetworking withNSURLSession in my toolbox?" That's the question I plan to answer in this article.

不管怎样,AFNetworking 都是一个传说。

正如 Mattt Thompson 在 NSHipster 上指出的,AFNetworking 实际上已经成为了 Cocoa 应用中不可或缺的一个开源库。当下还在不停的维护着,这个开源库已经获得了超过10,000人的支持,被fork了将近3,000多次。

如果,你还没听过 AFNetworking (哥真的鄙视你-_-!!),想了解一下他发展的历史以及一些牛逼的特性,这样的话,哥建议你读一下 Mattt 的博文AFNetworking 2.0 on NSHipster 。  在今天你看的这篇博文中,我将重点聚焦在两个方面,AFNetworking 2.0 有哪些新的特性,它与 NSURLSession API 之间到底有啥区别。你应该问一下你自己:“我是否还要用 AFNetworking 配合 NSRULSession 来处理网络呢?”那就是哥要回到你的问题。

Requirements(一些条件

Before we take a closer look at what AFNetworking 2.0 has to offer, it's important to know about the library's new requirements. It shouldn't be a surprise that the requirements of AFNetworking 2.0 are stricter than those of AFNetworking 1.x. As its version number indicates, AFNetworking includes breaking changes, which are the result of its new architecture. This also means that migrating a project from AFNetworking 1.x to 2.0 should be done with caution. The migration guide is the ideal place to start if you plan on migrating a project to AFNetworking 2.0.

AFNetworking no longer supports iOS 4.3 and 5. The minimum deployment targets for iOS and OS X are iOS 6 and OS X 10.8 respectively. AFNetworking 2.0 also requires Xcode 5. If you're still using Xcode 4, this may be a good time to make the transition to Xcode 5.

在我们进一步了解 AFNetworking 2.0 之前,知道这个开源库有着怎样的新的环境要求非常重要。正如他的版本号标示的那样子,AFNetworking 2.0 比 AFNetworking 1.x 有着更严格的限制条件,它有着许多全新的架构级的改变,和以前已经面目全非了。也就是说,你不要手贱把以前的 AFNetworking 1.x 的项目升级到 AFNetworking 2.0,否则会出现很多的警告,如果你确实想升级一下,恩,migration guide 这个指南应该是你的菜。

Solid Foundation(基础介绍

AFURLConnectionOperation

As many of you probably know, AFNetworking is built on top of NSURLConnection and NSOperation, which has proven to be a powerful and elegant combination. The result of this combination is AFURLConnectionOperation, an NSOperation subclass that manages anNSURLConnection instance and implements the NSURLConnectionDelegate protocol. Even though this robust foundation remains unchanged in AFNetworking 2.0, it is supplemented by the NSURLSession API, which I'll cover in more detail later.

也许你已经知道了, AFNetworking 是在 NSURLConnection 以及 NSOperation 的基础上封装的,现在已经证明, NSURLConnection  NSOperation 的组合使用非常的优雅.这个组合的结果就是 AFURLConnectionOperation ,一个 NSOperation 的子类来管理一个 NSURLConnection 的实例,并且实现协议NSURLConnectionDelegate.这种健壮的组合,即使是升级到了 AFNetworking 2.0 也没有改变,而 NSURLSession API 这货就是补缺的,直到现在才知道这么玩,后面我会详细的解说.

Separation of Responsibilities

In AFNetworking 1.x, the AFHTTPRequestOperation class was in charge of request and response serialization and validation. This is no longer true in AFNetworking 2.0. The AFHTTPRequestOperation class and its subclasses are no longer directly responsible for serializing and validating requests and responses. This responsibility has been moved to the AFURLRequestSerialization andAFURLResponseSerialization protocols. Each protocol declares a single delegate method to handle request and response serialization and validation respectively.

在 AFNetworking 1.x 中, AFHTTPRequestOperation 是用来管理网络请求 request 以及网络回复 response 的.在 AFNetworking 2.0 中,已经不再是这样子的了. AFHTTPRequestOperation 以及它的子类不会直接管理他们.操作以及回复被移到了 AFURLRequestSerialization 以及AFURLResponseSerialization 协议中.每一个协议定义了一个方法,用来管理请求以及回复.

To make your life easier, however, AFNetworking 2.0 ships with AFHTTPRequestSerializer and AFHTTPResponseSerializer as well as a number of subclasses for common content types, such as JSON and XML. To see how this works in practice, let's take a look at an example in which I query the Forecast API. This doesn't look all that different from how you made a request with AFNetworking 1.x. The main difference is that you'll be using the AFHTTPRequestOperation class more often. The AFJSONRequestOperation andAFXMLRequestOperation classes are no longer present in AFNetworking 2.0.

为了让你写代码写得更爽,AFNetworking 2.0 将 AFHTTPRequestSerializer AFHTTPResponseSerializer 以及通用的一些数据类型如 JSON 和 XML 等捆绑在一起.让我们来看一个例子,这个例子中我来调用一个天气预报的接口.在与使用 AFNetworking 1.x 比较时,不会有太大的区别,主要的不同就是你会经常的使用 AFHTTPRequestOperation,在 AFNetworking 2.0 中,AFJSONRequestOperation  AFXMLRequestOperation 已经被移除了.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
NSString   *key = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
NSURL   * URL   = [ NSURL   URLWithString :[ NSString   stringWithFormat : @"https://api.forecast.io/forecast/%@/37.8267,-122.423" ,   key ]];
 
// Initialize Request Operation
AFHTTPRequestOperation   *requestOperation = [[ AFHTTPRequestOperation   alloc ]   initWithRequest :[ NSURLRequest   requestWithURL :URL]];
 
// Configure Request Operation
[requestOperation   setResponseSerializer :[ AFJSONResponseSerializer   serializer ]];
[requestOperation   setCompletionBlockWithSuccess :^( AFHTTPRequestOperation   *operation, id   responseObject) {
     // Process Response Object
 
}   failure :^( AFHTTPRequestOperation   *operation, NSError   *error) {
     // Handle Error
}];
 
// Start Request Operation
[requestOperation   start ];

The AFJSONResponseSerializer class inherits from AFHTTPResponseSerializer and should be used for JSON responses. If you need to process XML responses, then AFXMLNResponseSerializer will help you out. For other content types, you need to subclassAFHTTPResponseSerializer.

AFJSONResponseSerializer 这个类继承至 AFHTTPResponseSerializer ,他是用来解析 JSON的,如果你需要解析XML数据,那这个AFXMLNResponseSerializer 就是你的菜.对于一些其他的数据类型,你需要继承他们的子类就行了.

AFHTTPRequestOperationManager

Another major change in AFNetworking 2.0 is the removal of the AFHTTPClient class, which was in charge of talking to web services. The responsibilities of AFHTTPClient have been split up into a number of classes and protocols. To talk to a web service, you now useAFHTTPRequestOperationManager and AFHTTPSessionManager. Just like AFHTTPRequestOperation, AFHTTPRequestOperationManager andAFHTTPSessionManager delegate serialization of requests and responses to separate objects. Both AFHTTPRequestOperationManager andAFHTTPSessionManager have a requestSerializer and responseSerializer property for this purpose. Let's revisit the above example to see how this works in practice.

AFNetworking 2.0 另外一个改变就是移除了 AFHTTPClient , AFHTTPClient 是用来与web服务器交互的, AFHTTPClient 的功能已经被分割成一系列的类和协议了.为了与 web 服务器交互,如今,你需要使用 AFHTTPRequestOperationManager 和 AFHTTPSessionManager.就像 AFHTTPRequestOperationAFHTTPRequestOperationManager AFHTTPSessionManager 以及一系列的协议那样来处理不同的请求与对象.AFHTTPRequestOperationManager AFHTTPSessionManager 都有 requestSerializer 以及 responseSerializer 这个属性,让我们再来看看上面的例子.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
NSString   *key = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
NSURL   *baseURL = [ NSURL   URLWithString :[ NSString   stringWithFormat : @"https://api.forecast.io/forecast/%@/" ,   key ]];
 
// Initialize Request Operation Manager
AFHTTPRequestOperationManager   *manager = [[ AFHTTPRequestOperationManager   alloc ]   initWithBaseURL :baseURL];
 
// Configure Request Operation Manager
[manager   setResponseSerializer :[ AFJSONResponseSerializer   serializer ]];
 
// Send Request
[manager   GET : @"37.8267,-122.423"   parameters :nil   success :^( AFHTTPRequestOperation   *operation, id   responseObject) {
     // Process Response Object
 
}   failure :^( AFHTTPRequestOperation   *operation, NSError   *error) {
     // Handle Error
}];

What About NSURLSession?(什么是NSURLSession

AFURLSessionManager

The key question when discussing AFNetworking 2.0 in the light of the NSURLSession API is whether we still need AFNetworking. AFNetworking adds a number of convenience classes and methods to the NSURLSession API and this is only possible thanks to the separation of responsibilities I discussed earlier. The most significant improvement that AFNetworking provides on top of theNSURLSession API are the AFURLSessionManager and AFHTTPSessionManager classes.

核心问题中,我们讨论了是否还需要使用 AFNetworking 2.0 ,AFNetworking 添加了很多方便的方法到 NSURLSession 的 API 中,在上文中我们讨论过了,这个网络多种回复的分开是很有益处的.最为核心的改进就是,AFNetworking 在 NSURLSession API 之上提供了AFURLSessionManager AFHTTPSessionManager 这两个类.

The AFURLSessionManager helps you manage an NSURLSession object. Even though I love the NSURLSession API, I have to agree with Mattt that its API is incomplete. The AFURLSessionManager class adds a number of convenience methods that make the NSURLSession API even better. Serialization and validation, for example, are much easier and intuitive with AFNetworking. Just like theAFHTTPRequestOperationManager, instances of AFURLSessionManager have a requestSerializer and responseSerializer property that makes serialization of requests and responses seamless and intuitive.

AFURLSessionManager 帮助你管理 NSURLSession 对象.虽然哥很喜欢 NSURLSession 的 API ,但是,正如 Mattt说说,这个 API 是不完美的.AFURLSessionManager 添加了一系列的便利的方法来扩展了 NSURLSession 的 API. 就像使用 AFHTTPRequestOperationManager 那样,实例化一个AFURLSessionManager 对象将有 requestSerializer  responseSerializer 属性,他们可以让序列化的网络请求以及网络回复无缝衔接,非常方便.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
NSString   *key = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
NSURL   * URL   = [ NSURL   URLWithString :[ NSString   stringWithFormat : @"https://api.forecast.io/forecast/%@/37.8267,-122.423" ,   key ]];
 
// Initialize Session Configuration
NSURLSessionConfiguration   *sessionConfiguration = [ NSURLSessionConfiguration   defaultSessionConfiguration ];
 
// Initialize Session Manager
AFURLSessionManager   *manager = [[ AFURLSessionManager   alloc ]   initWithSessionConfiguration :sessionConfiguration];
 
// Configure Manager
[manager   setResponseSerializer :[ AFJSONResponseSerializer   serializer ]];
 
// Send Request
NSURLRequest   *request = [ NSURLRequest   requestWithURL :URL];
[[manager   dataTaskWithRequest :request   completionHandler :^( NSURLResponse   *response, id   responseObject, NSError   *error) {
     // Process Response Object
}]   resume ];

I hope you can see the benefit of using AFNetworking in combination with the NSURLSession API. It makes working with theNSURLSession API much more intuitive especially if you're already used to the way AFNetworking 2.0 separates serialization and validation from the actual task or request operation.

我希望你能明白 AFNetworking 合并了 NSURLSession API的好处.它让使用 NSURLSession API 更加的直观,尤其是,你已近习惯了 AFNetworking 2.0 分割序列化以及确认的操作,便利于当前的网络操作.

AFHTTPSessionManager

AFNetworking 2.0 also incudes a subclass of AFURLSessionManager, AFHTTPSessionManager, which makes interacting with web services a breeze. The AFHTTPSessionManager class includes a number of conveniences methods, such as GET:parameters:success:failure: andPOST:parameters:constructingBodyWithBlock:success:failure: that make the migration process from AFHTTPClient to AFHTTPSessionManagereasy. Similar methods are also available in the AFHTTPRequestOperationManager class, which I discussed earlier.

AFNetworking 2.0 也有子类叫 AFURLSessionManager , AFHTTPSessionManager ,它让与服务器交互变得小菜一碟. AFHTTPSessionManager 这个类包含了一些便利的方法,例如, GET:parameters:success:failure: 以及 POST:parameters:constructingBodyWithBlock:success:failure: ,它衔接了从AFHTTPClient  AFHTTPSessionManager 的操作,使其更容易使用,同样的, AFHTTPRequestOperationManager 也有这些,后面讨论.

Reachability(网络是否畅通

Reachability is managed by the AFURLRequestOperationManager and AFURLSessionManager classes. When instances of these classes have a valid baseURL, then a reachability manager is automatically instantiated and set up. Of course, it is also possible to explicitly create an instance of the AFNetworkReachabilityManager class.

AFURLRequestOperationManager  AFURLSessionManager 这些类已经管理了网络是否畅通了,当实例化这些对象后,传了一个合法的 URL 地址,那么,监测网络就会自动执行.当然,你也可以直接就显示的实例化 AFNetworkReachabilityManager 来达到目的.

Icing On The Cake(锦上添花

AFNetworking 2.0 has a number of other features, such as built-in support for SSL pinning and various categories on UIKit classes. Take a look at this example in which I leverage AFNetworking's category on UIProgressView to update a progress view while downloading a remote image.

AFNetworking 2.0 有着很多其他的特性,例如内置支持 SSL pinning 以及一系列的类目扩展 UIKit,来看一下下面这个例子把,我用AFNetworking的类目来促使 UIProgressView 更新下载进度,当正在下载一张图片时.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
// Initialize Request
NSURLRequest   *request = [ NSURLRequest   requestWithURL :[ NSURL   URLWithString : @"http://cdn.tutsplus.com/mobile/uploads/2014/01/5a3f1-sample.jpg" ]];
 
// Initialize Session Manager
self .sessionManager   = [[ AFURLSessionManager   alloc ]   initWithSessionConfiguration :[ NSURLSessionConfiguration defaultSessionConfiguration ]];
 
// Initialize Download Task
NSURLSessionDownloadTask   *downloadTask = [ self .sessionManager   downloadTaskWithRequest :request   progress :nil   destination :^ NSURL   *( NSURL *targetPath,  NSURLResponse   *response) {
     // Handle Success
 
}   completionHandler :^( NSURLResponse   *response,  NSURL   *filePath,  NSError   *error) {
     // Handle Failure
}];
 
[ self .progressView   setProgressWithDownloadProgressOfTask :downloadTask   animated : YES ];
 
[downloadTask   resume ];
 

Verdict(裁决

If your project doesn't rely on networking, then you won't need all the power and convenience that AFNetworking has to offer. As a matter of fact, I think it's important that every iOS or OS X developer is familiar with NSURLSession and NSURLConnection, because they are key components of the platform.

如果你的项目中不依赖于网络,那么你或许就不需要 AFNetworking 给你提供的这些强大的功能.但是,实际上,我认为,对于每一个iOS 和 OSX的开发者而言,熟悉 NSURLSession 以及 NSURLConnection 非常有必要,因为他们是这个开发平台的一个核心模块.

Even though the NSURLSession API is great and easy to use, the networking APIs provided by the Foundation framework aren't perfect. As Mattt points out, even the NSURLSession API has a few notable gaps. AFNetworking tries to elegantly fill these gaps. The combination of the NSURLSession API and AFNetworking is a marriage made in heaven. It truly is a joy to use.

虽然 NSURLSession 的 API 很牛逼而且很好用,但是,Foundation framework 提供的网络相关的 API 还是不完美, Mattt 指出,NSURLSession API 有着一些缺陷, 正是 AFNetworking 来弥补了这些,将 NSURLSession API 以及 AFNetworking 组合在一起真是天上一对地上一双.用起来会让你非常的爽.

I really like the NSURLSession API and I hope I've convinced you of its power in this series. However, this doesn't mean that AFNetworking has become obsolete. In my opinion, AFNetworking is the perfect companion of the NSURLSession API. AFNetworking has matured and it is indeed the best and most robust networking library available. Its modular architecture means that you don't need to include every class or category it has to offer. You are free to pick and choose from the components that it includes. AFNetworking supports CocoaPods subspecs, which make this process trivial.

我真的喜欢 NSURLSession API ,我也确信我已经传达了它牛逼的地方.这个不是说 AFNetworking 就变得不那么重要了.我的观点是,AFNetworking 是NSURLSession API 最好的搭档.AFNetworking 以及成熟了,而且,他也确实是目前最成熟的网络框架.它那模块化的架构意味着,你不需要包含它全部的文件到你的工程当中.你可以自由的挑选你想用的组件到你的工程当中.

Conclusion(结论

I couldn't be more happy with the second major release of AFNetworking. The library's modularity and separation of responsibilities is what makes it shine in my opinion. It complements the NSURLSession API so well that you won't even notice that you're using AFNetworking. Mattt Thompson and the more than 130 contributors of AFNetworking have done a formidable job. It's good to be a Cocoa developer.

对于第二个版本的 AFNetworking 的释放,哥真的非常的高兴.这个库的模块化以及不同部分分担不同的职责让它十分的出众.它补充了 NSURLSession API ,让你察觉不到你正在使用 AFNetworking, Mattt Thompson 以及 130 个贡献代码的人做了一个令人敬佩的工作.作为苹果的开发人员,哥真的很幸福.

 
目录
相关文章
|
4月前
|
Swift iOS开发
Alamofire和AFNetworking有什么区别?
Alamofire和AFNetworking有什么区别?
63 4
|
4月前
|
Swift iOS开发 Perl
如何在项目中使用Alamofire和AFNetworking?
如何在项目中使用Alamofire和AFNetworking?
26 3
|
Java API
如何通过命令行使用Wisdom RESTClient?
如何通过命令行使用Wisdom RESTClient?
1145 0