如何使用NSURLCache在离线时返回缓存的API响应(iOS App)

如何使用NSURLCache在离线时返回缓存的API响应(iOS App)

问题描述:

我希望有人可以对我正在研究的一些事情有所了解,但没有取得多大进展。

I'm hoping someone can shed some light on a few things I've been researching but not making much progress on.

我想利用NSURLCache返回我在iOS App中进行的API调用的缓存响应。当设备在线时,如果它已经足够,我想返回缓存的响应,否则从远程获取。当设备离线时,我想立即返回缓存的响应(如果有的话),无论它的年龄如何。

I'd like to take advantage of NSURLCache to return cached responses for API calls that I make within an iOS App. When the device is online, I'd like to return the cached response if it's recent enough, otherwise fetch from remote. When the device is offline I'd like to immediately return the cached response (if any) regardless of it's age.

我正在使用 AFNetworking 。我正在调用的API调用是我控制的服务器。协议是HTTPS。 Cache-Control 响应标头当前是max-age = 0,public。我目前没有设置缓存相关的请求标头(我应该吗?)。我在离线时将请求的缓存策略设置为 NSURLRequestReturnCacheDataDontLoad ,并在联机时使用默认的 NSURLRequestUseProtocolCachePolicy 。我可以在磁盘上的默认 Cache.db 中看到请求及其响应。但是,当我离线时,所有请求都会失败(没有缓存的响应(尽管看起来已被缓存)正在使用/返回。

I'm using AFNetworking. The API calls that I'm making are to a server I control. Protocol is HTTPS. The Cache-Control response header is currently "max-age=0, public". I'm not currently setting cache related request headers (should I?). I set the request's cache policy to be NSURLRequestReturnCacheDataDontLoad when offline and use the default NSURLRequestUseProtocolCachePolicy when online. And I can see the requests and their responses in the default Cache.db on disk. However when I'm offline all requests fail (no cached responses (despite appearing to have been cached) are being used/returned.

Per http://nshipster.com/nsurlcache/ 我在 didFinishLaunchingWithOptions sharedURLCache / code>并将AFNetworking setCacheResponse 块设置为以下内容:

Per http://nshipster.com/nsurlcache/ I initialize a sharedURLCache in didFinishLaunchingWithOptions and set the AFNetworking setCacheResponse block to something like this:


NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy]; 
NSMutableData *mutableData = [[cachedResponse data] mutableCopy]; 
NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowed;
return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] data:mutableData userInfo:mutableUserInfo storagePolicy:storagePolicy];

我已经阅读了有关该主题的这些和其他帖子:

I've read these and other posts on the topic:

HTT p://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/
http://blackpixel.com/blog/2012/05/caching-and-nsurlconnection.html

我想知道在使用标准 NSURLCache 之前是否有人成功实现了此功能(也对涉及 SDURLCache 但Peter S.说从iOS5开始支持磁盘缓存,因此不再需要 SDURLCache ,即我想使用默认的内置缓存)。

I'm wondering if anyone out there has successfully achieved this functionality before using the standard NSURLCache (also interested in success stories involving SDURLCache but Peter S. says as of iOS5 disk caching is supported therefore SDURLCache is no longer needed, ie I'd like to use the default built in cache).

提前致谢!

你看过这篇文章了吗?

Did you see this post?

AFNetworking (AFHttpClient)离线模式无法使用NSURLRequestReturnCacheDataDontLoad策略

看起来它可能是iOS 6中的错误。这是Robert Mao在发布:

Looks like it might be a bug with iOS 6. Here is what Robert Mao had to say in the post:


汇总解决方法是从缓存中读取响应,而不是
使用NSURLRequestReturnCacheDataDontLoad策略:

A summarized work around is read the response from cache, instead of use NSURLRequestReturnCacheDataDontLoad policy:



NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache]     cachedResponseForRequest:request];
if (cachedResponse != nil &&
    [[cachedResponse data] length] > 0)
{
    // Get cached data
    ....
}