Tuesday, 25 April 2017

NSURLSession & Main difference between NSURLSession and NSURLConnection


NSURLSession
NOTE : (NSURLConneciton is Deprecated in OS X 10.11 and iOS 9.0)
  • 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.
  • 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.
  • 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
  • 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 

defaultSessionConfiguration 

Creates a default configuration object that uses the disk-persisted global cache, credential and cookie storage objects.

ephemeralSessionConfiguration 

Similar to the default configuration, except that all session-related data is stored in memory. Think of this as a “private” session.

 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

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.

Upload Tasks (NSURLSessionUploadTask)

Upload tasks are used to upload data to a remote destination. We can use NSURLSessionUploadTask.

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


No comments:

Post a Comment