在网络上使用Alamofire而不是NSURLSession/NSURLConnection有什么优势? [英] what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

查看:120
本文介绍了在网络上使用Alamofire而不是NSURLSession/NSURLConnection有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我理解以下问题:使用Alamofire相比NSURLSession/NSURLConnection有什么优势?

Can anyone help me in understanding these question : What is the advantage of using Alamofire over NSURLSession/ NSURLConnection?

NSURLSession和NSURLConnection有什么区别?

What are the differences between NSURLSession and NSURLConnection?

推荐答案

NSURLConnection是Apple用于进行联网(例如发出HTTP请求和接收响应)的旧API,而NSURLSession是它们的新API.后者是较高级别的,通常更容易使用,涉及的模板代码更少,对于大多数应用程序开发人员而言,这几乎是没有必要的-除了您不想更新的遗留代码外,基本上没有理由使用前者.

NSURLConnection is Apple's old API for doing networking (e.g. making HTTP requests and receiving responces), while NSURLSession is their new one. The latter one is higher level and is generally much easier and involves less boilerplate code to use for most application developers - there's basically no reason to use the former except in legacy code that you don't want to update.

一段历史:在NSURLSession出现之前,第三方库AFNetworking已成为在Objective C中进行网络连接的事实上的标准,因为它提供了比NSURLConnection更加轻松便捷的API(这是一个包装器) -我认为这些天来,它包装了NSURLSession).后来,同一位开发人员为Swift创建了一个类似的库,称为AlamoFire,以充分利用"Swift的做事方式"(而不仅仅是向AFNetworking添加Swift绑定).大约在同一时间,NSURLSession出现了,很显然,苹果公司受到了AFNetworking的启发,并希望使他们的新网络API变得与使用起来一样方便-总的来说,我认为他们成功了.这意味着,尽管以前,对于大多数开发人员而言,使用AFNetworking代替NSURLConnection是唯一明智的选择,但如今,使用AFNetworking或AlamoFire而不是NSURLSession的优势要小得多,对于大多数启动新项目的开发人员,我建议首先使用NSURLSession,只有在他们感到遇到某种限制或不便的情况下才调查AlamoFire,这些限制或不便之处足以证明添加另一个依赖项是合理的.

A bit of history: Before NSURLSession came out, a third party library, AFNetworking, became the de facto standard for doing networking in Objective C, as it provided an easier and more convenient API than NSURLConnection (which it was a wrapper around - I think these days it wraps NSURLSession instead). The same developer later made a similar library for Swift, called AlamoFire in order to fully take advantage of "the Swift way of doing things" (instead of just adding Swift bindings to AFNetworking). Around the same time NSURLSession came out and it seemed pretty obvious that Apple had been inspired by AFNetworking and wanted to make their new networking API just as convenient to work with - by and large I think they succeded. What this means is that while previously, using AFNetworking instead of NSURLConnection was the only sane choice for most developers, today the advantages of using AFNetworking or AlamoFire over NSURLSession are much smaller, and for most developers starting up new projects I'd recommend to just start by using NSURLSession and only look into AlamoFire if they feel that they run into some limitation or inconvenience that is big enough to justify adding another dependency.

这篇关于在网络上使用Alamofire而不是NSURLSession/NSURLConnection有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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