ASIDownloadCache未使用新内容进行更新

时间:2012-03-15 13:10:44

标签: iphone objective-c ios4 asihttprequest

以下是使用ASIHTTPRequest从远程服务器下载和缓存图像的源代码。如果服务器未使用图像的缓存副本更新新图像。它在初始请求中缓存所有图像,并且工作正常。

根据以下代码,它应该检查服务器是否每次都有新内容可用。我检查了服务器日志,它打印304状态代码。但是当我使用最新图像更新时,它不会显示新图像。而不是它显示缓存的图像。但服务器日志表示它正在通过状态码200发送新图像。为了避免这种情况,我必须删除该应用并重新安装它。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.timeOutSeconds = kDefaultTimeInterval;
request.validatesSecureCertificate = NO;
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setSecondsToCache:kSecondsToCache];
[request startSynchronous];

if (![request didUseCachedResponse]) {
    NSLog(@"  Cache miss.  Loading resource %@", location);
} else {
    NSLog(@"  Cache hit!");
}
NSLog(@" ***  image data size: %u bytes", [request.responseData length]);
return ([request error]) ? nil : request.responseData;

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

最后,我自己想出了那里发生的事情。这只是一个愚蠢的错误和对API的误解。

通过秒缓存,我认为这是为了在设置值之前保持缓存。但似乎不是。根据文件

Set secondsToCache on the request to override any expiry date for the content
set by the server, and store this response in the cache until secondsToCache 
seconds have elapsed

所以它超越了服务器设置的值。因此,即使服务器使用新内容进行更新,并且为请求设置的到期日期未过期,也将使用本地副本。

作为修复,我刚刚删除了行[request setSecondsToCache:kSecondsToCache];

相关问题