NSURLSession - Imtiaz211/interviews GitHub Wiki
NSURLSession Task
* NSURLSessionDataTask: Use this task for HTTP GET requests to retrieve data from servers to memory.
* NSURLSessionUploadTask: Use this task to upload a file from disk to a web service, typically via a HTTP POST or PUT method.
* NSURLSessionDownloadTask: Use this task to download a file from a remote service to a temporary file location.
* NSURLSessionTask: is an abstract class that denotes a task object. A session creates a task, which does the actual work of fetching data
and downloading or uploading files."
What is the difference between NSURLConnection and 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.
* NSURLConnection if you suddenly realized you needed to do those things, you had to refactor your code to not use block-based callbacks."
What is the difference between NSURLConnection and 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.NSURLConnection
if you suddenly realized you needed to do those things, you had to refactor your code to not use block-based callbacks.
NSURLSession
also provides support for
background
downloads
, which make it possible tocontinue 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. It alsoprovides grouping of related requests
, making it easy tocancel all
of the requests associated with a particular work unit, such ascanceling all
loads associated with loading a web page when the user closes the window or tab. It 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.
NSURLSessionDelegate
- An object that coordinates a group of related network data transfer tasks.
- URLSession:dataTask:didReceiveResponse:completionHandler
- URLSession:dataTask:didBecomeDownloadTask
- URLSession:dataTask:didBecomeStreamTask
- URLSession:dataTask:didReceiveData
- URLSession:dataTask:willCacheResponse:completionHandler
- GET: to get the resource.
- POST: to insert.
- PUT: to update.
- DELETE: to delete.
URLSession Configuration?
default
: Creates a default configuration object that uses the disk-persisted global cache, credential and cookie storage objects.ephemeral
: Similar to the default configuration, except that you store all of the session-related data in memory. Think of this as a “private” session.background
: Lets the session perform upload or download tasks in the background. Transfers continue even when the app itself is suspended or terminated by the system.