[翻译] 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 about
AFNetworking 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 with NSURLSession 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 an
NSURLConnection 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 and AFURLResponseSerialization
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 and AFXMLRequestOperation 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 subclass
AFHTTPResponseSerializer.

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 use
AFHTTPRequestOperationManager and AFHTTPSessionManager. Just like
AFHTTPRequestOperation, AFHTTPRequestOperationManager and
AFHTTPSessionManager delegate serialization of requests and responses
to separate objects. Both AFHTTPRequestOperationManager and
AFHTTPSessionManager 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.就像 AFHTTPRequestOperation
AFHTTPRequestOperationManager 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 the NSURLSession 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 the
AFHTTPRequestOperationManager, 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 the NSURLSession 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: and
POST:parameters:constructingBodyWithBlock:success:failure: that make
the migration process from AFHTTPClient to AFHTTPSessionManager
easy. 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
独贡献代码的人做了一个使得人佩服的工作.作为苹果之开发人员,哥真的十分幸福.

 

 

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注