AFNetworking : How to know if response is using cache or not ? 304 or 200 AFNetworking : How to know if response is using cache or not ? 304 or 200 ios ios

AFNetworking : How to know if response is using cache or not ? 304 or 200


I think I found a solution to determine if response was returned from cache or not using AFNetworking 2.0. I found out that each time a new response is returned from the server (status 200, not 304) the cacheResponseBlock which is a property of AFHTTPRequestOperation is called. The block should return NSCachedURLResponse if response should be cached or nil if it shouldn't. That's way you can filter responses and cache only some of them. In this case, I am caching all responses that comes from the server. The trick is, that when server sends 304 and response is loaded from cache, this block won't be called. So, this is the code I am using:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];BOOL __block responseFromCache = YES; // yes by defaultvoid (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {    if (responseFromCache) {        // response was returned from cache        NSLog(@"RESPONSE FROM CACHE: %@", responseObject);    }    else {        // response was returned from the server, not from cache        NSLog(@"RESPONSE: %@", responseObject);    }};void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {    NSLog(@"ERROR: %@", error);};AFHTTPRequestOperation *operation = [manager GET:@"http://example.com/"                                      parameters:nil                                         success:requestSuccessBlock                                         failure:requestFailureBlock];[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {    // this will be called whenever server returns status code 200, not 304    responseFromCache = NO;    return cachedResponse;}];

This solution works for me and I haven't found any issues so far. But, if you have a better idea or some objections against my solution, feel free to comment!


seems that apple doesn't want you to know if it comes from cache or not.

I found a way by saving modification-date associate with the request, and I compare this date when AFNetWorking answers to me.

not as clean as I intend, but works...


There is a way to specify the status codes that should be treated as success in AFNetworking, it is done through response serialization, here is the code

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];AFHTTPResponseSerializer *respSerializer = [AFHTTPResponseSerializer serializer];NSMutableIndexSet *responseCodes = [NSMutableIndexSet indexSet];[responseCodes addIndex:200];[responseCodes addIndex:304];[operation setResponseSerializer:respSerializer];

With this code AFNetworking will treat 304 as success