用NSURLSession替换NSURLConnection [英] Replacing NSURLConnection with NSURLSession

查看:94
本文介绍了用NSURLSession替换NSURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为 NetworkCommunication 开始设计。我有一个设计 NSOperation 子类创建和管理他们自己的 NSURLConnection NSOperation 子类由 NetworkManger 类实例化,该类将其添加到 NSOperationQueue 。请求完成后,将调用委托(例如: ViewController )。这是流程:

I have been starting a design for NetworkCommunication. I had a design where NSOperation subclasses create and manage their own NSURLConnection. The NSOperation subclass is instantiated by a NetworkManger class which adds it to the NSOperationQueue. Once the request is finished the delegate(ex:ViewController will be called). This is the flow:

网络管理器实例化 NSOperation 子类(封装URL,参数等)并添加它它维持的 NSOperationQueue
NSOperation子类实例化 NSURLConnection (执行异步请求并检索数据)
NSURLConnection 将数据转储到NSOperation-subclass
NSOperation-subclass执行委托(视图控制器)提供的完成块。
我正在尝试用 NSURLSession 实现相同的模式。我希望能够封装在单个对象中发出网络请求所需的url和参数。

The Network Manger instantiates NSOperation subclass (which encapsulates URL, parameters etc) and adds it to a NSOperationQueue it maintains. NSOperation subclass instantiates NSURLConnection (which performs asynchronous request and retrieves data) NSURLConnection dumps data to NSOperation-subclass NSOperation-subclass executes the completion block supplied by the delegate( view controller). I'm trying to implement the same pattern with NSURLSession now. I want to be able to encapsulate the url and parameters required to make a network request inside a single object.

如果我使用相同的模式,是否可以使用 NSURLSession 。我检查了 AFNetworking 类。但是,他们没有为 NSURLSession 创建NSOperation子类。此外,会话对象应该去哪里。它是否是NSOperation课程的一部分。

Is it fine if i use the same pattern if I use NSURLSession. I checked AFNetworking classes. But, they have not subclassed NSOperation for NSURLSession. And, moreover where should the session object go. Will it be a part of the NSOperation class.

有人可以提供一些专家意见。我应该可以取消请求,上传(POST / PUT),下载数据。 Network Manager类将成为任何网络请求的单一联系点。

Can someone give some expert advice on this. I should be able to cancel requests, do uploading(POST/PUT), downloading of data. The Network Manager class would be the single point of contact for any network request.

推荐答案

更新:

NSURLConnection 已弃用有效的Mac OS 10.11和iOS 9.因此,此时 NSURLSession 应该用来代替 NSURLConnection 。正如 NSURLConnection.h 标题所示:

NSURLConnection is deprecated effective Mac OS 10.11 and iOS 9. So, at this point NSURLSession should be used in lieu of NSURLConnection. As the NSURLConnection.h header says:


已弃用:不应再使用 NSURLConnection 类。 NSURLSession NSURLConnection 的替代品。

Deprecated: The NSURLConnection class should no longer be used. NSURLSession is the replacement for NSURLConnection.

我的原始答案如下。

答案取决于你是否需要丰富的各种 NSURLSession 委托方法与否。如果您可以使用完成块再现(即没有进度回调,没有流等),则从 NSURLConnection 转换为 NSURLSession 非常简单。只需将 NSURLSession 实例放入 NetworkManager 类中,然后根据 NSURLSessionTask <包装代理/ code>并发 NSOperation 子类中的实例,你就完成了。只需采用标准的异步/并发 NSOperation 子类模式。

The answer depends upon whether you need the richness of the various NSURLSession delegate methods or not. If you're ok using the completion block renditions (i.e. no progress callbacks, no streaming, etc.), the conversion from NSURLConnection to NSURLSession is pretty trivial. Just put your NSURLSession instance in your NetworkManager class, and then wrap the delegate based NSURLSessionTask instances in a concurrent NSOperation subclass and you're done. Just adopt the standard asynchronous/concurrent NSOperation subclass pattern.

如果你正在使用基于委托的再现 NSURLSession ,这是一个完全不同的鱼。主要的麻烦是在会话委托而不是任务委托对象上调用各种 NSURLSessionTask 委托方法。乍一看,这可能听起来像是一个微不足道的问题,但是如果您的操作实例具有唯一的完成/进度块,那么您就会遇到如何让会话对象将这些委托方法回调映射到个人的麻烦原始请求操作实例。

If you're using the delegate-based renditions of NSURLSession, it's a whole different kettle of fish. The main hassle is that the various NSURLSessionTask delegate methods are called on the session delegate rather than a task delegate object. At first blush, this might sound like it's a trivial issue, but if your operation instances have unique completion/progress blocks, for example, you're stuck with the hassle of how to have the session object map these delegate method callbacks to the individual original request operation instances.

要解决此问题,您必须维护任务标识符到 NSOperation 子类的映射对象。然后,您可以在相应的 NSOperation 子类中实现这些 NSURLSessionTask (包括任务,下载任务和上载任务)委托方法。然后, NSURLSession 网络管理器类在收到 NSURLSessionTask 委托调用时,可以使用任务标识符来标识相应的 NSOperation 实例,然后在那里调用相应的委托方法。

To tackle this, you have to maintain a mapping of task identifiers to your NSOperation subclass objects. You can then implement these NSURLSessionTask (including task, download task, and upload task) delegate methods at the respective NSOperation subclass. The NSURLSession network manager class can then, when it receives a NSURLSessionTask delegate call, use the task identifier to identify the appropriate NSOperation instance, and then call the appropriate delegate method there.

最后,如果你打算处理背景 NSURLSession 实例,生活变得更加困难(因为即使你的应用程序终止并且所有对象都被丢弃后,后台任务仍将继续)。后台会话根本不适合基于 NSOperation 的方法。

Finally, if you intend to handle background NSURLSession instances, life gets even more difficult (because background tasks will continue even after your app has terminated and all of its objects have been discarded). Background sessions simply don't lend themselves to an NSOperation-based approach.

底线,这是微不足道的你只需要完成块 NSURLSession 方法,但如果你需要基于委托的再现,这有点麻烦。

Bottom line, this is trivial if you only need the completion-block NSURLSession methods, but it's a bit of a hassle if you need the delegate-based rendition.

这篇关于用NSURLSession替换NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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