gpt4 book ai didi

iPhone:磁盘上的 NSURLCache

转载 作者:行者123 更新时间:2023-12-03 20:24:20 33 4
gpt4 key购买 nike

我在将 URLResponse 缓存到磁盘时遇到问题。我想要这样,因为我正在下载的数据需要在满足 http header 中的过期标签时重新加载,而不是更早。另外,我希望 iPod touch 用户能够在线下载一次数据,然后离线使用。

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

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 在评论中正确指出:

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017152/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com