Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the difference among NSURLConnection, NSURLSession and AFNetworking? [closed]

Tags:

ios

I want to know that what are the basic differences among them and what is the best and common method for managing webservices in iOS end in Objective-c and swift.

As previously I worked in Objective-c and when we need to use webservices we are using AFNetworking and NSURLConnections, As NSURLSession launches I moved to work with this one in swift. So which one is suitable, so I can able to manage all webservices network connections conditions etc?

like image 895
Gourav Joshi Avatar asked Nov 11 '16 06:11

Gourav Joshi


1 Answers

Difference among NSURLConnection, 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.

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 provide a 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.

Difference among NSURLConnection, NSURLSession and AFNetworking?

NSURLConnection and NSURLRequest are the provided Cocoa classes for managing connections. In iOS 7, Apple added NSURLSession.

But I think you'll find AFNetworking to be a framework that further simplifies network requests (notably complex HTTP requests). If you don't want to use third-party frameworks for any reason, you can use NSURLConnection and/or NSURLSession directly. It just takes a little more coding.

For information on NSURLConnection and NSURLSession see the URL Loading System Programming Guide.

Find from reference1 and reference2

Thank rob and dgatwood for such an amazing Answer...

like image 105
Hitesh Surani Avatar answered Nov 14 '22 23:11

Hitesh Surani