19

Is it possible to disable all the cache features from AFNetworking?

I am building my own custom cache system and don't want this to take up disk space too.

Thanks, Ashley

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
Ashley Staggs
  • 1,557
  • 9
  • 24
  • 38

6 Answers6

25

Initially I found Jason Moore's answer to work, however recently I have noticed my app is still caching requests. I am not using the latest AFNetworking, so I do not know if caching has been addressed in more recent builds.

Apple's URLCache Project has this to say:

By default, the Cocoa URL loading system uses a small shared memory cache. We don't need this cache, so we set it to zero when the application launches.

And then does this to disable the cache.

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                        diskCapacity:0
                                                            diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

This will disable all caching for your whole app, which may not be ideal in some situations, but as NSURLRequest is not honouring the requested cache policy, it is the only option left that I can see.

addicted2oxygen
  • 391
  • 4
  • 11
  • 1
    worked for me even with AFnetworking - caches were corrupting resumed download from https service ... – PetrV Jul 11 '13 at 22:21
25

Cacheing is handled application-wide by NSURLCache. If you don't set a shared cache, requests are not cached. Even with a shared NSURLCache, the default implementation on iOS does not support disk cacheing anyway.

That said, unless you have a very particular reason to write your own cacheing system, I would strongly recommend against it. NSURLCache is good enough for 99.9% of applications: it handles cache directives from incoming responses and uses them appropriately with new requests, and does so automatically in a way that is unlikely to be a performance bottleneck in your application. As someone who has wasted untold hours making one myself (and later throwing it away since it was completely unnecessary), I'd say that there are much better places to focus your development attention.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • 3
    is there any way to disable it still? I really need this as my cache system is a bit unorthodox anyway. It's a real must have to disable the cache. – Ashley Staggs Apr 08 '12 at 19:34
  • It's off by default. You have to opt-in by creating an `NSURLCache` and setting that as the shared cache. – mattt Apr 08 '12 at 19:52
  • 4
    Is it already? Currently it looks like it will be autogenerated for you. https://github.com/AFNetworking/AFNetworking/blob/experimental-1.0RC1/AFNetworking/UIImageView%2BAFNetworking.m#L70 – steipete Apr 08 '12 at 22:23
  • I assumed that `AFImageCache` wasn't really what OP meant by cacheing. If that is the case though, the solution would be not to use the `UIImageView` category, I guess. – mattt Apr 08 '12 at 23:49
  • I cover this in the following blog post - thanks! http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/ – vfxdrummer Mar 14 '14 at 18:44
  • @mattt do you have any ideas on [this question](http://stackoverflow.com/questions/28755444/ios-app-getting-stale-data-from-rest-api-until-browser-hits-same-api) mattt? I'm using AFNetworking which has worked amazingly so far and I'm not sure if my issue is related to your library or not. I have cachePolicy set to ignore local and remote cache. – Cameron Askew Feb 27 '15 at 01:34
23

I've found on iOS 6 that requests are sometimes cached, even if the request has NSURLRequestReloadIgnoringCacheData. Adding a cache response block that returns nil prevents the request from being cached.

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                              cachePolicy:NSURLRequestReloadIgnoringCacheData
                                          timeoutInterval:20];
AFJSONRequestOperation *op =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request];

// DISABLE CACHE //
[op setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
    return nil;
}];

[op start];
Jason Moore
  • 7,169
  • 1
  • 44
  • 45
  • I second this. Thanks for the solution! – Firefly May 22 '13 at 12:13
  • 4
    For using the `AFHTTPClient`, we should override `requestWithMethod:path:parameters` .Call super method to get the `NSURLRequest` and say `request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;` – Yeung Jun 19 '13 at 03:17
  • Could you please fix the wrong type of enum in your code snippet? It's gonna be miss leading for anyone who doesn't notice there are two updates. – Greg Wang Jan 22 '14 at 05:10
  • Setting the cache policy to NSURLCacheStorageNotAllowed is incorrect. You are using a NSURLCacheStoragePolicy value instead of a NSURLRequestCachePolicy. What effectively happens is that you set it to NSURLRequestReturnCacheDataElseLoad which has the same value (2) as NSURLCacheStorageNotAllowed. – Maciej Swic Sep 12 '14 at 10:09
  • I just updated the answer to remove the incorrect enum. Sorry for the confusion - clearly editing the answer is better than adding updates! – Jason Moore Sep 12 '14 at 13:37
  • According to the NSURLRequest headers, NSURLRequestReloadIgnoringLocalAndRemoteCacheData is unimplemented, so I wouldn't rely on it. You may want to use NSURLRequestReloadIgnoringLocalCacheData instead. – Michael Long Feb 19 '16 at 22:21
10

Unfortunately non of these methods worked for me. The only way I've managed to get the cache disabled is this way:

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

Here's the original answer

Community
  • 1
  • 1
JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • I have a singleton to manage Backend calls. I put these two lines of code in my init method and all work well! – Zeb Sep 29 '15 at 07:53
0
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
JAL
  • 41,701
  • 23
  • 172
  • 300
chings228
  • 1,859
  • 24
  • 24
0

This may depend on your server implementation, but adding Cache-Control:no-store header to your outgoing request should result in a response that contains the same header, thus commanding NSURLCache to not cache the response to disk.

IMHO this is a better approach than disabling disk-caching completely, especially if you're writing SDK code that will be used by other applications which may want to utilize NSURLCache.

Yuri Brigance
  • 542
  • 4
  • 13