Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the biggest difference between NSURLConnection and NSURLSession

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

like image 984
Forrest Avatar asked Jan 23 '15 08:01

Forrest


People also ask

What is the difference between NSURLSession and 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 ...

What is NSURLSession?

Overview. The NSURLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn't running or, in iOS, while your app is suspended.

What is NSURLConnection in Swift?

An NSURLConnection object lets you load the contents of a URL by providing a URL request object. The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request. You perform most of your configuration on the URL request object itself. Note.


1 Answers

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.

like image 178
dgatwood Avatar answered Oct 11 '22 19:10

dgatwood