17

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?

Stunner
  • 12,025
  • 12
  • 86
  • 145
soleil
  • 12,133
  • 33
  • 112
  • 183
  • did you solve this? I can't find something to set cache policy in AFHTTPRequestOperationManager – laucel Jan 10 '14 at 09:57
  • I cover caching in AFNetworking in this blog post - AFImageCache & NSURLCache. Maybe you will find it use flu : http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/ – vfxdrummer Mar 14 '14 at 18:45
  • try like this: http://stackoverflow.com/questions/33767908/volunteermatch-api-objective-c/33886449#33886449 – Vvk Feb 19 '16 at 16:31

8 Answers8

61

Make long story short, just define your AFNetworking manager:

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

Enjoy!

gran33
  • 12,421
  • 9
  • 48
  • 76
  • 1
    There is no such method named setCachePolicy for AFNetworking. Could you please make it more clear? – Aswin Sathyan Jul 09 '15 at 08:50
  • 2
    With the new AFNetworking library versions you can set this to url session configuration object. `NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;` – dishan Feb 29 '16 at 05:15
12

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.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
1

Just do:

manager.requestSerializer.cachePolicy = NSURLRequestCachePolicyReturnCacheDataElseLoad
Phiter
  • 14,570
  • 14
  • 50
  • 84
Pranshu
  • 430
  • 6
  • 13
1

For Swift poeple

let manager = AFHTTPSessionManager()
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
1

With AFNetworking 3:

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL
                                                         sessionConfiguration:sessionConfiguration];
Mick F
  • 7,312
  • 6
  • 51
  • 98
1

try to add some rubbish at the end of your URL (for example, timestamp)

kDataUrl = [NSString stringWithFormat:@"%@?%f", kDataUrl, [NSDate timeIntervalSinceReferenceDate]];

In this case, you would request fresh data every time. That works for me))

dollar2048
  • 426
  • 6
  • 10
1
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"no-store" forHTTPHeaderField:@"Cache-Control"];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

Adding the Cache-Control:no-store header to the request, assuming your server is implemented correctly, will return a response with the same header thus disabling NSURLCache disk cache for any request that contains this header.

Yuri Brigance
  • 542
  • 4
  • 13
0

I don't feel good about it, but here is what works. In the AppDelegate didFinishLaunchingWithOptions:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                            diskCapacity:0
                                                                diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
soleil
  • 12,133
  • 33
  • 112
  • 183