iphone:磁盘上的 NSURLCache [英] iphone: NSURLCache on disk

查看:51
本文介绍了iphone:磁盘上的 NSURLCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 URLResponse 缓存到磁盘时遇到问题.我想要那个,因为我正在下载的数据需要在它的 http-header 中的 expires-tag 满足时重新加载,而不是更早.此外,我希望 iPod touch 用户能够在线下载数据,然后离线使用.

I'm having trouble with caching URLResponses to disk. I want that because the data i'm downloading needs reloading when the expires-tag in it's http-header is met, not earlier. Also, I want iPod touch-Users to be able to download the data once while online and then use it offline.

我正在这样做,它可以很好地将内容缓存在内存中,但在重新启动应用程序时失败:

i'm doing this which works fine for caching things in memory but fails when relaunching the app:

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 缓存到磁盘.

AFAIK NSURLRequest/NSURLConnection support caching to disk from iOS 5.

更新:@Rob 在评论中正确说明:

UPDATE: @Rob correctly states in the comments:

我发现如果 (a) 你使用默认的 NSURLRequest cachePolicyNSURLRequestUseProtocolCachePolicy;(b) 响应没有包含 Cache-Control 标头,它不会缓存到磁盘.如果你使用一个NSURLRequestReturnCacheDataElseLoad 的 cachePolicy,或者如果响应来自服务器的指定特定的 Cache-Control 标头(例如public, max-age=1835400),它将缓存到持久存储中.

I've found that if (a) you use the default NSURLRequest cachePolicy of NSURLRequestUseProtocolCachePolicy; and (b) the response doesn't include the Cache-Control header, it won't cache to disk. If you use a cachePolicy of NSURLRequestReturnCacheDataElseLoad, or if the response from the server specifies a particular Cache-Control header (e.g. public, max-age=1835400), it will cache to persistent storage.

这篇关于iphone:磁盘上的 NSURLCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆