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
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
A group of the interrelated components that form the Foundation URL Loading System: NSURLRequest, NSURLResponse, NSURLProtocol, NSURLCache, NSHTTPCookieStorage, NSURLCredentialStorage, and its namesake, NSURLConnection
NSURLRequest objects are passed to an NSURLConnection object. The delegate (conforming to the erstwhile informal and protocols) responds asynchronously as an NSURLResponse, and any associated NSData are sent from the server
Before a request is sent to the server, the shared cache is consulted, and depending on the policy and availability, a cached response may be returned immediately and transparently. If no cached response is available, the request is made with the option to cache its response for any subsequent requests. In the process of negotiating a request to a server, that server may issue an authentication challenge, which is either handled automatically by the shared cookie or credential storage, or by the connection delegate. Outgoing requests could also be intercepted by a registered NSURLProtocol object to seamlessly change loading behavior as necessary.
NSURLSession
refers to a group of interdependent classes, in addition to the eponymous class NSURLSession. NSURLSession is comprised of the same pieces as before, with NSURLRequest, NSURLCache, and the like, but replaces NSURLConnection with NSURLSession, NSURLSessionConfiguration, and three subclasses of NSURLSessionTask: NSURLSessionDataTask, NSURLSessionUploadTask, and NSURLSessionDownloadTask.
NSURLSessionTask is an abstract subclass, with three concrete subclasses that are used directly: NSURLSessionDataTask, NSURLSessionUploadTask, and NSURLSessionDownloadTask. These three classes encapsulate the three essential networking tasks of modern applications: fetching data, such as JSON or XML, and uploading and downloading files.for more
Difference between NSURLSession and NSURLConnection
NSURLSession
NOTE : (NSURLConneciton is Deprecated in OS X 10.11 and iOS 9.0)
1) 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.
2) NSURLSession also provides support for background downloads,which make it possible to continue downloading resources while your app is not running or when it is in the background on iOS.
3) NSURLSession also provides grouping of related requests,Making is easy to cancel all of the requests associated with a particular work unit, such as canceling 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
4) NSURLSession also provides nicer interfaces for requesting data using blocks,n that it allows you to combine them with delegate methods for doing custom authentication handling, redirect handling, etc.
NSURLSessionConfiguration Types
1) defaultSessionConfiguration
Creates a default configuration object that uses the disk-persisted global cache, credential and cookie storage objects.
2) ephemeralSessionConfiguration
Similar to the default configuration, except that all session-related data is stored in memory. Think of this as a “private” session.
3) backgroundSessionConfiguration
Lets the session perform upload or download tasks in the background. Transfers continue even when the app itself is suspended or terminated
Types of NSURLSessionTasks
1) Data tasks (NSURLSessionDataTask)
Data tasks are used for requesting data from a server, such as JSON data. These data are usually stored in memory and never touches the File System We can use NSURLSessionDataTask.
2) Upload Tasks (NSURLSessionUploadTask)
Upload tasks are used to upload data to a remote destination. We can use NSURLSessionUploadTask.
3)Download Tasks (NSURLSessionDownloadTask)
Downloading a file and Storing in a temporary location. We can use NSURLSessionDownloadTask.
Main difference between NSURLSession and NSURLConnection
NSURLConnection:
if we have an open connection with NSURLConnection and the system interrupt our App, when our App goes to background mode, everything we have received or sent were lost.
NSURLSession
solve this problem and also give us out of process downloads. It manage the connection process even when we don’t have access. You will need to use
application:handleEventsForBackgroundURLSession:completionHandler in your AppDelegate
For Details information please refer difference between NSURLSession and NSURLConnection