Understanding AFNetworking - Cache
Caching
I’m talking about AFHTTPSessionManager
, but the same applies to AFHTTPRequestOperationManager
. AFNetworking is just a wrapper around AFURLSession
and AFURLConnection
, and uses NSURLCache
to make caching happen
Does AFNetworking cache
AFNetworking only uses NSCache for caching images, as implemented in AFImageCache. Read more How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
Usage
We often usage AFHTTPSessionManager
like this
1 |
[AFHTTPSessionManager manager] |
Which is using default session configuration
1 |
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration { |
NSURLSessionConfiguration
The defaultSessionConfiguration
has 2 cache properties
URLCache
The URL cache for providing cached responses to requests within the session.
This property determines the URL cache object used by tasks within sessions based on this configuration.
To disable caching, set this property to nil.
For default sessions, the default value is the shared URL cache object.
For background sessions, the default value is nil.
For ephemeral sessions, the default value is a private cache object that stores data in memory only, and is destroyed when you invalidate the session.
requestCachePolicy
A predefined constant that determines when to return a response from the cache.
This property determines the request caching policy used by tasks within sessions based on this configuration.
Set this property to one of the constants defined in NSURLRequestCachePolicy to specify whether the cache policy should depend on expiration dates and age, whether the cache should be disabled entirely, and whether the server should be contacted to determine if the content has changed since it was last requested.
The default value is NSURLRequestUseProtocolCachePolicy.
dataTaskWillCacheResponse
Sets a block to be executed to determine the caching behavior of a data task
AFNetworking uses this block as a chance to us to intercept to the response
1 |
- (void)URLSession:(NSURLSession *)session |
Reading URLSession:dataTask:willCacheResponse:completionHandler
tells us that
The session calls this delegate method after the task finishes receiving all of the expected data. If you do not implement this method, the default behavior is to use the caching policy specified in the session’s configuration object. The primary purpose of this method is to prevent caching of specific URLs or to modify the userInfo dictionary associated with the URL response.
The session calls this delegate method after the task finishes receiving all of the expected data. If you do not implement this method, the default behavior is to use the caching policy specified in the session’s configuration object. The primary purpose of this method is to prevent caching of specific URLs or to modify the userInfo dictionary associated with the URL response.
- This method is called only if the NSURLProtocol handling the request decides to cache the response. As a rule, responses are cached only when all of the following are true:
- The request is for an HTTP or HTTPS URL (or your own custom networking protocol that supports caching).
- The request was successful (with a status code in the 200–299 range).
- The provided response came from the server, rather than out of the cache.
- The session configuration’s cache policy allows caching.
- The provided NSURLRequest object’s cache policy (if applicable) allows caching.
- The cache-related headers in the server’s response (if present) allow caching.
- The response size is small enough to reasonably fit within the cache. (For example, if you provide a disk cache, the response must be no larger than about 5% of the disk cache size.)
How to prevent caching
We can prevent caching by either
- Implement
dataTaskWillCacheResponse
and return nil - Set
requestCachePolicy
toNSURLRequestReloadIgnoringLocalCacheData
1
2AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
sessionManager.session.configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
Note that NSURLRequestReloadIgnoringLocalAndRemoteCacheData
is not implemented by the OS, and NSURLRequestReloadIgnoringCacheData
is just synonym for NSURLRequestReloadIgnoringLocalCacheData
-
Set
URLCache
to nil1
2AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
sessionManager.session.configuration.URLCache = nil; -
NSURLRequest cachePolicy
NSURLRequest hascachePolicy
that can be used to control caching for specific request
Reference
- Caching and NSURLConnection