Tob*_*ary 2 iphone objective-c
我在将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];
}
Run Code Online (Sandbox Code Playgroud)
AFAIK NSURLRequest/NSURLConnection支持从iOS 5缓存到磁盘.
更新:@Rob在评论中正确陈述:
我发现如果(a)你使用NSURLRequestUseProtocolCachePolicy的默认NSURLRequest cachePolicy; (b)响应不包括Cache-Control标头,它不会缓存到磁盘.如果您使用NSURLRequestReturnCacheDataElseLoad的cachePolicy,或者如果来自服务器的响应指定了特定的Cache-Control标头(例如public,max-age = 1835400),它将缓存到持久存储.
| 归档时间: |
|
| 查看次数: |
5214 次 |
| 最近记录: |