Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - do not cache response

I'm using this code to pull a simple JSON feed from a server:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager GET:kDataUrl parameters:nil
 success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"response: %@", responseObject);
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"JSON DataError: %@", error);
 }];

It works. However, after I change the JSON file at kDataUrl, and verify that the change is made in a browser, when I run the app again, I still get the previous response.

It seems that AFNetworking is somehow caching the old response. I do not want this behavior. I want to download the current feed. Is there some kind of setting or parameter I need to set to turn off caching?

like image 720
soleil Avatar asked Nov 23 '13 18:11

soleil


4 Answers

Make long story short, just define your AFNetworking manager:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

Enjoy!

like image 57
gran33 Avatar answered Nov 10 '22 18:11

gran33


What you are experiencing is the effect of the URL cache (see NSURLCache).

The caching behavior of the request can be defined by setting a "Cache Policy" for the NSMutableURLRequest object, e.g.:

NSMutableURLRequest* request = ...;
[request setCachePolicy: myCachePolicy];

The default caching behavior (NSURLRequestUseProtocolCachePolicy) is appropriate for the current protocol, which is HTTP. And for the HTTP protocol, a GET requests will be cached by default!

And, AFNetworking does not change the default behavior of the request!

Now, you could set another cache policy, for example:

NSURLRequestReloadIgnoringLocalCacheData

Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

This is likely the desired behavior you want to achieve:

[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];

The problem here is, that the super "convenient" API does not provide a way to configure the URL cache behavior of the request. You cannot access the used request at all.

Thus, I would suggest to use a lower level API where you have control about the created NSMutableURLRequest object, and set the cache policy accordingly.

like image 28
CouchDeveloper Avatar answered Nov 10 '22 18:11

CouchDeveloper


Just do:

manager.requestSerializer.cachePolicy = NSURLRequestCachePolicyReturnCacheDataElseLoad
like image 1
Pranshu Avatar answered Nov 10 '22 19:11

Pranshu


For Swift poeple

let manager = AFHTTPSessionManager()
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
like image 1
AITAALI_ABDERRAHMANE Avatar answered Nov 10 '22 18:11

AITAALI_ABDERRAHMANE