NSURLConnection,NSURLSession和AFNetworking有什么区别? [英] What are the difference among NSURLConnection, NSURLSession and AFNetworking?

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

问题描述

我想知道它们之间的基本差异是什么,以及在Objective-c和swift中管理iOS端Web服务的最佳和常用方法是什么。

I want to know that what are the basic differences among them and what is the best and common method for managing webservices in iOS end in Objective-c and swift.

如前所述,我在Objective-c工作,当我们需要使用Web服务时,我们正在使用AFNetworking和NSURLConnections,当NSURLSession启动时,我开始在swift中使用这个。
那么哪一个适合,所以我能够管理所有webservices网络连接条件等?

As previously I worked in Objective-c and when we need to use webservices we are using AFNetworking and NSURLConnections, As NSURLSession launches I moved to work with this one in swift. So which one is suitable, so I can able to manage all webservices network connections conditions etc?

推荐答案

谢谢提出问题。

NSURLConnection 之间的区别, NSURLSession

Difference among NSURLConnection, NSURLSession

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

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 还提供对后台下载的支持,这样可以在您的应用无法继续下载资源时运行(或在iOS上运行时)。对于某些用例,这也是一个重大胜利。

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

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 还提供了使用块来请求数据的更好的接口,因为它允许您将它们与委托方法结合起来进行自定义身份验证处理,重定向处理等,而使用 NSURLConnection ,如果你突然意识到你需要做这些事情,你必须重构代码以不使用基于块的回调。

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 和AFNetworking?

Difference among NSURLConnection, NSURLSession and AFNetworking?

NSURLConnection NSURLRequest 是用于管理连接的Cocoa类。在iOS 7中,Apple添加了NSURLSession。

NSURLConnection and NSURLRequest are the provided Cocoa classes for managing connections. In iOS 7, Apple added NSURLSession.

但我认为你会发现 AFNetworking 是一个进一步简化网络请求(特别是复杂的HTTP请求)的框架。如果您不想出于任何原因使用第三方框架,可以使用 NSURLConnection 和/或 NSURLSession 直。它只需要更多的编码。

But I think you'll find AFNetworking to be a framework that further simplifies network requests (notably complex HTTP requests). If you don't want to use third-party frameworks for any reason, you can use NSURLConnection and/or NSURLSession directly. It just takes a little more coding.

有关 NSURLConnection NSURLSession 请参见网址加载系统编程指南。

查找reference1 reference2

感谢抢劫 dgatwood 获得如此惊人的答案......

Thank rob and dgatwood for such a amazing Answer...

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

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