4

in iOS 7 cachePolicy doesn't work, it just cache the downloaded json.

//URLRequest
        NSString *url = [NSString stringWithFormat:@"http://www.semhora.com/jsonparser/categories/categories_%d_test.json", _categoriesIndex];
        NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                                  cachePolicy:NSURLCacheStorageNotAllowed
                                          timeoutInterval:60.0];

How can I disallow cache in iOS 7?

Marckaraujo
  • 7,002
  • 11
  • 50
  • 92

3 Answers3

7

I encountered the same problem and I verified that setting cachePolicy = 0 instead of cachePolicy = NSURLCacheStorageNotAllowed fixes the problem.

This doesn't make sense to me either since 0 corresponds to NSURLCacheStorageAllowed.
We can't just set it to 0 since Apple will probably fix this in a future release.
You might try calling:

[NSURLCache sharedURLCache] removeCachedResponseForRequest:yourRequest] just before initiating the request.

UPDATE: After further research I found that the code that broke has been using the wrong enum. Take a look at NSURLRequestCachePolicy in NSURLRequest.h. That's the one you need and it explains why setting to 0 fixed the problem for you.

Dan
  • 71
  • 2
6

I just used:

//URLRequest
        NSString *url = [NSString stringWithFormat:@"http://www.semhora.com/jsonparser/categories/categories_%d_test.json", _categoriesIndex];
        NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                                  cachePolicy:0
                                          timeoutInterval:60.0];

And now it works, got any answer from apple dev forum until now why it happens.

Marckaraujo
  • 7,002
  • 11
  • 50
  • 92
2

The correct enum for the cache Policy is :

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:downloadURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];

If you are over 3G, some provider use caching even if you disable it in your NSMutableURLRequest, so if the cache policy doesn't work set the HTTP header field cache-control to no-cache.

[request setValue:@"no-cache" forHTTPHeaderField:@"cache-control"];
Volker E.
  • 5,443
  • 11
  • 43
  • 62
Benpaper
  • 124
  • 4
  • 5
    Be aware that as of iOS 8.1, the cache policy `NSURLRequestReloadIgnoringLocalAndRemoteCacheData` is still unimplemented as described in the NSURLRequest header file in the Foundation framework. – Thomas Verbeek Dec 04 '14 at 22:52