Long story short, I just updated to Xcode 6 to check how my app works on iOS 8. I noticed that it doesn't use the cache even though it should. I'm using AFNetworking setting the cachePolicy like this:
sessionManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
I still have an iOS 7 device in which I tested the same code and it works there as expected.
Does anyone have a solution for this, or do we need to wait for Apple to fix it?
NSURLConnection is not working properly. It will store responses properly, but it never purges the cache (diskCapacity is ignored) on iOS 8.0 so the cache will grow without limit, though manually clearing the cache does work. This is fixed in iOS 8.1, although removeCachedResponseForRequest: still does not work.
Also, if you specify a cache size smaller than 5 megs, it will not cache anything.
Both
NSURLCache
andNSURLSession
are buggy. Even if you setNSURLRequestReturnCacheDataElseLoad
for request, iOS may try to reload file from server. It happens for example when cached response hasVary
header. Also please notice that methoduse
User-Agent
header from request to distinguish cached objects. It means iOS will store the same file twice, if you access it by NSURLSession and UIWebView, becauseUser-Agent
is different.I'm almost certain that iOS 8.0 has broken
NSURLSession
's ability to cache HTTP response data. I've opened a radar with Apple about this issue.Here's some sample code I wrote to prove this:
Even creating your own
NSURLSession
-- with anNSURLSessionConfiguration
that has anNSURLCache
that you create yourself -- won't fix this issue. For now, if you need cached responses badly, you have to useNSURLConnection
.