NSURLConnection与NSURLSession之间的最大区别是什么? [英] What is the biggest difference between NSURLConnection and NSURLSession

查看:164
本文介绍了NSURLConnection与NSURLSession之间的最大区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSURLSession 是新网络SDK,而不是 NSURLConnection 。第三个旧选择是CFNetwork。



这里的问题是要弄清楚它们之间的最大区别,以了解Apple为何会像这样发展。



谢谢

解决方案

整个模型不同。 NSURLSession的设计基于这样的假设:你会有很多需要类似配置的请求(标​​准的标题集等),如果你这样做会让生活变得更加轻松。



NSURLSession还支持后台下载,这样可以在您的应用未运行时(或在iOS上的后台运行时)继续下载资源。对于某些用例,这也是一个重大胜利。



NSURLSession还提供相关请求的分组,从而可以轻松取消与特定工作单位关联的所有请求,例如当用户关闭窗口或标签时取消与加载网页相关的所有加载。



NSURLSession还提供了使用块请求数据的更好界面,因为它允许您将它们与委托方法结合使用以进行自定义身份验证处理,重定向处理等,而使用NSURLConnection,如果您突然意识到需要执行这些操作,则必须重构代码以避免使用基于块的回调。 / p>

NSURLSession is new network SDK than NSURLConnection from Apple. 3rd old choice is CFNetwork.

Question here is to figure out the biggest difference between them to understand why Apple is evolving like these.

Thanks

解决方案

The entire model is different. NSURLSession is designed around the assumption that you'll have a lot of requests that need similar configuration (standard sets of headers, etc.), and makes life much easier if you do.

NSURLSession also provides support for background downloads, which make it possible to continue downloading resources while your app isn't running (or when it is in the background on iOS). For some use cases, this is also a major win.

NSURLSession also provides grouping of related requests, making it easy to cancel all of the requests associated with a particular work unit, such as canceling all loads associated with loading a web page when the user closes the window or tab.

NSURLSession also provides nicer interfaces for requesting data using blocks, in that it allows you to combine them with delegate methods for doing custom authentication handling, redirect handling, etc., whereas with NSURLConnection, if you suddenly realized you needed to do those things, you had to refactor your code to not use block-based callbacks.

这篇关于NSURLConnection与NSURLSession之间的最大区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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