AFNetworking vs. Na2ve NSURLConnec2on & NSURLSession + Caching
What is AFNetworking? AFNetworking is a class developed by MaD Thompson and ScoD Raymond and is described as “a delighHul networking library for iOS and Mac OS X.” It makes common tasks like asynchronous hDp requests a lot easier. AFNetworking just like all other popular libraries to do the same all use NSURLConnec2on to do the actual 'work'. They just include a bunch of friendly methods so you can easily make requests without having to write a whole lot of boilerplate code yourself.
Several AFNetworking beneﬁts - ‐ One of the beneﬁts of using AFNetworking is the data type classes for handling response data. The success block has already parsed the response and returns the data for you. With NSURLSession you receive NSData back in the comple2on handler, so you would need to convert the NSData into JSON or other formats. - ‐ It also helps you ensure that your UI is responsive even when your app is in the middle of a big download. - ‐ AFNetworking adds a category to UIImageView that lets you load images asynchronously, meaning the UI wil remain responsive while images are downloaded in the background.
What is NSURLSession? In iOS 7, Apple has introduced NSURLSession, which is a suite of classes that replaces NSURLConnec-on as the preferred method of networking. Using NSURLSession is just as easy as using its predecessor NSURLConnec-on for simple tasks.
Several NSURLSession new advantages and beneﬁts
- ‐ NSURLSession is created, you get all the beneﬁts of background networking. This helps with baDery life, supports UIKit mul2tasking and uses the same delegate model as in- ‐ process transfers. - ‐ Ability to pause and resume networking opera-ons: With the NSURLSession API any networking task can be paused, stopped, and restarted. No NSOpera2on sub- ‐classing is necessary. - ‐ Instead of storing all of the networking objects (such as a response cache) globally, NSURLSession provides a mechanism for storing objects either on a global basis or on a per session basis.
- ‐ Rich delegate model: NSURLConnec-on has some asynchronous block based methods, however a delegate cannot be used with them. When the request is made it either works or fails, even if authen2ca2on was needed. With NSURLSession you can have a hybrid approach, use the asynchronous block based methods and also setup a delegate to handle authen2ca2on. - ‐ Uploads and downloads through the ﬁle system: This encourages the separa2on of the data (ﬁle contents) from the metadata (the URL and se]ngs).
Let’s compare! NSURLConnec-on NSURLSession
AFNetworking A simple request to API/Server using completion blocks.
Web Caching A web cache is a mechanism for the temporary storage (caching) of web documents, such as HTML pages and images, to reduce bandwidth usage, server load, and perceived lag. A web cache stores copies of documents passing through it. A client, such as a web browser, can store web content for reuse. For example : if the back buDon is pressed, the local cached version of a page may be displayed instead of a new request being sent to the web server.
How do we cache? NSURLCache provides a composite in- ‐memory and on- ‐disk caching mechanism for URL requests to your applica2on. As part of Founda2on's URL Loading System, any request loaded through NSURLConnec2on wil be handled by NSURLCache. Network caching reduces the number of requests that need to be made to the server, and improve the experience of using an applica2on oﬄine or under slow network condi2ons. When a request has ﬁnished loading its response from the server, a cached response wil be saved locally. The next 2me the same request is made, the locally- ‐cached response wil be returned immediately, without connec2ng to the server. NSURLCache returns the cached response automa&cal y and transparently.
NSURLRequestCachePolicy Caching policies are speciﬁed in both the request (by the client) and in the response (by the server). NSURLRequest has a cachePolicy property, which speciﬁes the caching behavior of the request according to the fol owing constants: NSURLRequestUseProtocolCachePolicy = default behavior. NSURLRequestReloadIgnoringLocalCacheData = don’t use the cache. NSURLRequestReloadIgnoringLocalAndRemoteCacheData = seriously don’t use the cache. NSURLRequestReturnCacheDataElseLoad = Use the cache no ma9er how out of date, or if no cached response exists, load from the network. NSURLRequestReturnCacheDataDontLoad = Oﬄine mode. Always use the cache no ma9er how out of date. NSURLRequestReloadRevalida-ngCacheData = Validate cache against server before using.
Conclusion NSURLSession can do every NSURLConnection can do and AFNetworking can do every NSURLConnect NSURLConnection and NSURLSession can do. But you can also do AFNetworking can do using NSURLConnection and NSURLSession NSURLSession just a little bunch of codes.
All of them are capable of using Cache policy in communication of server Cache- AFNetworking Control header.
AFNetworking has a great category method in UIImageView that automatical y request the image and cache it while the app is running.