NSURLRequest cache issue iOS 7

﹥>﹥吖頭↗ 提交于 2019-12-12 09:41:30

问题


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?


回答1:


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.




回答2:


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.




回答3:


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"];


来源:https://stackoverflow.com/questions/18923675/nsurlrequest-cache-issue-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!