Cache-Control: max-age does not make AFNetworking cache the response

| | August 7, 2015

On the server side, I’m setting Cache-Control: max-age=14400.

From my iOS client, I’m setting up a shared instance of AFHTTPSessionManager like follows:

+ (TKClient *)sharedInstance {
static TKClient *instance = nil;
static dispatch_once_t token;

dispatch_once(&token, ^ {
    instance = [[TKClient alloc] initWithBaseURL:[NSURL URLWithString:kTKBaseUrl]];
    [instance setRequestSerializer:[AFJSONRequestSerializer serializer]];
});

return instance;

And making my API calls like this:

- (void)listingsWithParams:(NSDictionary *)params completion:(void (^)(NSMutableArray *listings, NSError *error))completion {
[self GET:@"api/listings" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    // success here
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    // error here
}];

I have verified that the Cache-Control header is being returned properly from the server side for this listings call. However, the call goes to the server every time, and it’s clearly not being cached. Any idea what the problem could be?

Do I need to configure something else on the iOS side, or change the Cache-Control headers some way? Thanks!

One Response to “Cache-Control: max-age does not make AFNetworking cache the response”

  1. Try to add this:

    [connectionManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
    

    Edit
    check the headers again maybe you are missing something on the API if you’ve done everything!
    maybe you should use "Cache-Control" = "public, max-age=14400"; add public keyword to the Cache-Control header?

    public Indicates that the response MAY be cached by any cache, even if it would normally be non-cacheable or cacheable only within a non- shared cache.

Leave a Reply