NSURLRequest iOS 7 cache error

in iOS 7 cachePolicy does not work, it just caches loaded 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 to disable cache in iOS 7?

+4
source share
3 answers

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 a response from the apple dev forum so far why this is happening.

+6
source

I ran into the same problem and made sure that setting cachePolicy = 0 instead of cachePolicy = NSURLCacheStorageNotAllowed fixes the problem.

This does not make sense to me, since 0 corresponds to NSURLCacheStorageAllowed .
We cannot just set it to 0, as Apple is likely to fix this in a future version.
You can try calling:

[NSURLCache sharedURLCache] removeCachedResponseForRequest:yourRequest] before starting the request.

UPDATE: After further research, I found that the broken code uses the wrong enumeration. Take a look at NSURLRequestCachePolicy in NSURLRequest.h. This is the one you need and it explains why setting to 0 fixed the problem for you.

+7
source

Proper enumeration for cache policy:

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

If you use 3G, some providers use caching, even if you disable it in your NSMutableURLRequest , so if the cache policy does not work, set the HTTP header field cache control to no-cache.

 [request setValue:@"no-cache" forHTTPHeaderField:@"cache-control"]; 
+2
source

All Articles