Afnetworking is the most commonly used network framework in daily development, now we use version 3.0, 3.0 and 2.0 The biggest difference is that Afnetworking 2.0 uses Nsurlconnection's underlying API, And 3.0 is completely based on the Nsurlsession API, has abandoned the nsurlconnection. Nsurlsession can be seen as a replacement for Nsurlconnection, and in 2013 the Apple Global Developers Conference (WWDC2013) was released with iOS7, a new network access interface that was optimized for nsurlconnection reconfiguration. Starting with iOS9.0, the two methods of sending requests in nsurlconnection have expired (synchronous requests, asynchronous requests) and Apple is no longer recommended. The following is a simple comparison of the differences between Nsurlconnection and Nsurlsession.
1. Common Tasks and uploads
Nsurlsession provides a dedicated solution for complex network operations such as download/upload, which corresponds to three different network request tasks for normal, upload, and download: Nsurlsessiondatatask, Nsurlsessionuploadtask and Nsurlsessiondownloadtask. The created task is suspended and requires a resume to execute.
When the data returned by the server is small, there is no difference between nsurlsession and nsurlconnection performing normal tasks.
When performing an upload task, nsurlsession and Nsurlconnection also need to set up the request body for the POST request to upload.
2. Download Task mode
Nsurlconnection Download the file, the entire file is downloaded to memory before writing to the sandbox, if the file is larger, there will be a memory spike. Downloading files using Nsurlsessionuploadtask will be downloaded to the TEM folder in the sandbox by default, without a memory spike, but after the download is complete, the temporary files in the TEM will be deleted, and when the task method is initialized, Add the code to save the file in the Completionhandler callback.
The following code is an instance of the network download task when you save the downloaded file to the caches folder in the sandbox:
[Nsurlsessiondownloadtask [Nsurlsessiondownloadtask *task = [session downloadtaskwithurl:[nsurl URLWithString:@ ' http ://127.0.0.1/dawenjian.zip "] completionhandler:^ (Nsurl * _nullable location, Nsurlresponse * _nullable response, Nserror * _nullable error) {
Get the caches path to the sandbox
NSString *path = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "KKK.DMG"];
Generate URL Path
Nsurl *dcurl = [Nsurl Fileurlwithpath:path];
Save the file to the specified file directory
[[Nsfilemanager defaultmanager]moveitematurl:location Tourl:dcurl Error:nil]; }]resume];
3. Control of the request method
Nsurlconnection instantiates an object, the instantiation begins, the default request is sent (synchronously sent), and no call to the Start method is required. While cancel can stop the request from being sent, it cannot continue to access after it has stopped, and a new request needs to be created.
Nsurlsession has three control methods, Cancel (cancel), pause (suspend), continue (resume), and can resume the current request task after pausing.
4. How the breakpoint continues to be transmitted
Nsurlconnection a breakpoint download, by setting the Range property of the access request Httpheaderfield, turn on the run loop, nsurlconnection the proxy method as the event source that runs the loop, When the download data is received, the proxy method continues to be called and the data is saved using the Nsoutputstream pipeline stream.
Nsurlsession a breakpoint download, when the download task is paused, if Downloadtask (download Task) is non-empty, call Cancelbyproducingresumedata: (void (^) (NSData *resumedata)) This completionHandler method, this method receives a parameter, completes the processing code block, this code block has a nsdata parameter resumedata, if Resumedata is not empty, we save this object to the view controller's Resumedata attribute. When you click Download again, the [[Self.session Downloadtaskwithresumedata:self.resumedata]resume] method is called to continue the download operation.
The above comparison can be found, using nsurlsession to make breakpoints download more convenient.
5. Configuration information
The Nsurlsession method (SessionWithConfiguration:delegate:delegateQueue) has a parameter in the Nsurlsessionconfiguration class that can set the configuration information, It determines the configuration of cookies, security and cache policies, maximum host connections, resource management, network timeouts, and so on. Nsurlconnection cannot make this configuration, Nsurlsession has a great improvement over nsurlconnection depending on a global configuration object and lack of flexibility.
Nsurlsession can set up three configuration information, respectively, by invoking three tired methods to return a configuration object:
+ (Nsurlsessionconfiguration *) defaultsessionconfiguration, configuration information Use persistent session cache based on hard disk, save user's certificate to keychain, use shared cookie storage;
+ (Nsurlsessionconfiguration *) ephemeralsessionconfiguration, configuration information is roughly the same as default. In addition, the cache, certificate, or any session-related data is not stored to the hard disk, but stored in memory, with the same lifecycle and session. such as browser non-trace browsing and other functions can be based on this to do;
+ (Nsurlsessionconfiguration *) Backgroundsessionconfigurationwithidentifier: (NSString *) identifier, Configuration information can create a session that can still transmit data in the background or even when the app is closed. Note that the background session must be created with a unique identifier, so that the next time the app runs, it can be based on the identifier to make the relevant distinction. If the user closes the App,ios system, all background sessions will be closed. Moreover, after being forcibly closed by the user, the iOS system does not actively wake the app, and the data transfer will continue only if the user launches the app the next time.
Due to the limited personal level, this article collation reference part of the information, the following is the reference address of the relevant information:
1.http://blog.csdn.net/djl4104804/article/details/22667273
2.http://demo.netfoucs.com/hello_hwc/article/details/44513699#
3.http://www.th7.cn/program/ios/201402/173926.shtml
The difference between iOS network access interface-nsurlsession and nsurlconnection