Cache-Control: max-age does not make AFNetworking caching an answer

On the server side, I set Cache-Control: max-age = 14400.

From my iOS client, I am setting up a generic AFHTTPSessionManager instance, for example:

+ (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 correctly returned from the server side for this list. However, the call accesses the server each time, and it is clearly not cached. Any idea what could be the problem?

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

+4
source share
1 answer

Try adding this:

 [connectionManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad]; 

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

public Indicates that the response can be cached by any cache, even if it is usually not cached or cached only in an nonequivalent cache.

+1
source

All Articles