[英]iphone: NSURLCache on disk
我在将URLResponses缓存到磁盘时遇到问题。 我想这样,因为我下载的数据需要重新加载,当它的http-header中的expires-tag满足时,而不是更早。 此外,我希望iPod touch-Users能够在线下载一次数据,然后离线使用。
我这样做是为了缓存内存中的东西,但在重新启动应用程序时失败:
NSURLRequest* menuRequest = [NSURLRequest requestWithURL:mensaURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
NSCachedURLResponse* cachedMenuResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:menuRequest];
if (cachedMenuResponse) {
// received data is a member of that class to which the asynchronous
// download writes and which is then being used in updateDataFromDownload
// to retrieve my data structure from the download.
self.receivedData = [NSMutableData dataWithData:[cachedMenuResponse data]];
[self updateDataFromDownload];
NSLog(@"using data from cache");
} else {
NSLog(@"opening connection");
[NSURLConnection connectionWithRequest:menuRequest delegate:self];
}
AFAIK NSURLRequest / NSURLConnection支持从iOS 5缓存到磁盘。
更新 :@Rob在评论中正确陈述:
我发现如果(a)你使用NSURLRequestUseProtocolCachePolicy的默认NSURLRequest cachePolicy; (b)响应不包括Cache-Control标头,它不会缓存到磁盘。 如果您使用NSURLRequestReturnCacheDataElseLoad的cachePolicy,或者如果来自服务器的响应指定了特定的Cache-Control标头(例如public,max-age = 1835400),它将缓存到持久存储。
SDURLCache是NSURLCache的一个很好的子类,用于磁盘缓存,非常注重性能。
希望这有帮助,文森特
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.