13

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

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.

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/ 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:

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

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).

Thanks in advance!

Alfie Hanssen
  • 16,366
  • 10
  • 64
  • 71
  • It appears this was fixed in iOS 7. Is there any official confirmation from Apple that this bug was dealt with? – hpique Mar 16 '14 at 11:47

2 Answers2

2

Did you see this post?

AFNetworking (AFHttpClient) offline mode not working with NSURLRequestReturnCacheDataDontLoad policy

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

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
    ....
}
Community
  • 1
  • 1
Del Brown
  • 928
  • 1
  • 7
  • 19
  • 1
    Linking to all articles related to this answer: [one](http://stackoverflow.com/questions/14002422/afnetworking-afhttpclient-offline-mode-not-working-with-nsurlrequestreturncach), [two](https://github.com/AFNetworking/AFNetworking/issues/378), [three](https://github.com/AFNetworking/AFNetworking/issues/566) – Alfie Hanssen Jul 15 '13 at 13:51
  • Thanks a lot Del, this is exactly what I was looking for. – Alfie Hanssen Jul 15 '13 at 13:55
-4

Unless all of your calls are 100% GET and free of side effects or time dependency then this is dangerous.

ahwulf
  • 2,564
  • 12
  • 27
  • 9
    This is probably more of a comment than an answer. Appreciate your input but there's some serious discussion around this topic because it is a legitimate use case and NSURLCache is in part designed to handle this sort of thing. – Alfie Hanssen Mar 29 '13 at 21:02