What is the biggest difference between NSURLConnection and NSURLSession

前端 未结 3 1207
再見小時候
再見小時候 2020-12-02 19:10

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 u

3条回答
  •  长情又很酷
    2020-12-02 19:43

    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

提交回复
热议问题