iOS Cache Policy

半城伤御伤魂 提交于 2019-11-30 14:56:55

问题


I am doing a connection to the NSURL and I need to create a request to bypass all the cache policy. I have seen examples such as :

NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:0 timeoutInterval:10];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

May I ask what does the cache policy 0 refers to? Have tried searching for what the number represents but I'm not getting any answer I need. Thanks! :)


回答1:


you should have a look at NSURLRequestCachePolicy enum, here 0 mean is NSURLRequestUseProtocolCachePolicy, that means NSURLRequest would not load data from server every time.

enum
{
   NSURLRequestUseProtocolCachePolicy = 0,
   NSURLRequestReloadIgnoringLocalCacheData = 1,
   NSURLRequestReloadIgnoringLocalAndRemoteCacheData =4,
   NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
   NSURLRequestReturnCacheDataElseLoad = 2,
   NSURLRequestReturnCacheDataDontLoad = 3,
   NSURLRequestReloadRevalidatingCacheData = 5
};
typedef NSUInteger NSURLRequestCachePolicy



回答2:


https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html

NSURLRequestUseProtocolCachePolicy = 0

Specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default policy for URL load requests.

Example: If you use HTTP the HTTP-Header fields will be evaluated to decide either or not caching should be used.

The right Policy in your case is:

NSURLRequestReloadIgnoringLocalCacheData




回答3:


According to this article: http://blackpixel.com/blog/2012/05/caching-and-nsurlconnection.html , if you are using NSURLRequestUseProtocolCachePolicy and the server does not return either expiration or max-age, the default cache time interval is 6 - 24 hours. So Be careful about this condition. It is a better practice to set max-age or expiration when using NSURLRequestUseProtocolCachePolicy.



来源:https://stackoverflow.com/questions/12381991/ios-cache-policy

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