我无法将URL响应缓存到磁盘。我希望这样做,因为我下载的数据需要在满足HTTP头中的expires标记时重新加载,而不是更早。此外,我希望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];
}