Contents
  1. 1. Caching
    1. 1.1. Does AFNetworking cache
    2. 1.2. Usage
    3. 1.3. NSURLSessionConfiguration
      1. 1.3.1. URLCache
      2. 1.3.2. requestCachePolicy
    4. 1.4. dataTaskWillCacheResponse
    5. 1.5. How to prevent caching
  2. 2. Reference

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
2
3
4
5
6
7
8
9
10
11
12
13
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super init];
if (!self) {
return nil;
}

if (!configuration) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
}

self.sessionConfiguration = 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
willCacheResponse:(NSCachedURLResponse *)proposedResponse
completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler
{
NSCachedURLResponse *cachedResponse = proposedResponse;

if (self.dataTaskWillCacheResponse) {
cachedResponse = self.dataTaskWillCacheResponse(session, dataTask, proposedResponse);
}

if (completionHandler) {
completionHandler(cachedResponse);
}
}

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

  1. Implement dataTaskWillCacheResponse and return nil
  2. Set requestCachePolicy to NSURLRequestReloadIgnoringLocalCacheData
    1
    2
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.session.configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

Note that NSURLRequestReloadIgnoringLocalAndRemoteCacheData is not implemented by the OS, and NSURLRequestReloadIgnoringCacheData is just synonym for NSURLRequestReloadIgnoringLocalCacheData

  1. Set URLCache to nil

    1
    2
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.session.configuration.URLCache = nil;
  2. NSURLRequest cachePolicy
    NSURLRequest has cachePolicy that can be used to control caching for specific request

Reference

  1. URL Loading System Programming Guide
  1. NSURLProtocol Tutorial
  1. NSURLCACHE & NSCACHED URLRESPONSE
  1. AFNetworking : How to know if response is using cache or not ? 304 or 200
  1. AFNetworking 2.0 - Forced caching
  1. How to disable caching from NSURLSessionTask
  1. AFNetworking different cache settings for different content
  1. Why is NSURLSession don’t use my configured NSURLCache?
  1. NSURLCache
  1. Caching and NSURLConnection
Contents
  1. 1. Caching
    1. 1.1. Does AFNetworking cache
    2. 1.2. Usage
    3. 1.3. NSURLSessionConfiguration
      1. 1.3.1. URLCache
      2. 1.3.2. requestCachePolicy
    4. 1.4. dataTaskWillCacheResponse
    5. 1.5. How to prevent caching
  2. 2. Reference