How to use offline cache with AFHTTPClient #378

xissburg opened this Issue Jun 14, 2012 · 7 comments


None yet
4 participants

I have an AFHTTPClient subclass where I perform a few JSON requests basically. I searched and read a lot of stuff about offline caching, I see that the Cache.db file is created (I am using NSURLCache in iOS 5), but I don't really know how to use the offline cache and I can't find this information anywhere. When offline a call to getPath:parameters:success:failure: fails with a 'no internet connection' error. Is there other method that I should use? Or what?



mattt commented Jun 14, 2012

See Apple's URL Loading Guide section about caching for details about how to use it.

NSURLCache is a nice way to speed up requests, but it's not something you should depend on for important bits. If you want specific objects persisting between launches, perhaps you should consider using something like NSCoding or CoreData.

Well, that document does not tell how to use it. I was expecting for the getPath:parameters:success:failure: succeed with the cached data while offline, but that does not happen. Like I said, the data is cached in the Cache.db (I inspected it with sqlite and the requested data is indeed there).


mattt commented Jun 14, 2012

Sorry, but I don't know what to tell you, then. There are a lot of factors when it comes to this, including HTTP headers and unknown implementation details about NSURLCache internals—for instance, if you're requesting over HTTPS or are on iOS 4, disk cache won't work. Stuff like that.

The HTTP cache is there to reduce the amount of network transfer that happens in your app. If you need guarantees beyond that, I would suggest that you use one of the methods I described before.

Heh. Ok. I will try something else. Thanks so much.

@xissburg xissburg closed this Jun 14, 2012

mave99a commented Dec 22, 2012

I set the NSURLRequestReturnCacheDataDontLoad policy in the request, and verified there are data in the cache, however the AFHttpClient getPath will always fail in the airplane mode.

If the phone is not in airplane mode, it seemed NSURLRequestReturnCacheDataDontLoad will return data in cache.

What's the correctly way to access the cached data from AFNetworking in offline mode? Thanks!


guykogus commented Dec 22, 2012

Please see #566 as I mentioned a bunch of research I had done regarding this issue. It's an iOS 6 bug. I've detailed a small workaround, so please read that thread.

mave99a commented Dec 22, 2012

Thanks! That's very helpful! I spent a whole night on this problem, you
saved my day!

On Saturday, December 22, 2012, guykogus wrote:

Please see #566 I mentioned a bunch of research I had done regarding this issue. It's an
iOS 6 bug. I've detailed a small workaround, so please read that thread.

Reply to this email directly or view it on GitHub

Robert Mao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment