Question

I'm creating an iOS app that needs to get a lot of information from a remote API. There are a lot of http requests that can be cached perfectly fine, but it seems NSURLConnection isn't caching aggressively enough: every time I kill the app and start it up again, all requests are made again, instead of coming from the cache (iOS 5 should use a disk cache). If I make the same request a second time (within the app lifetime), it does come from the cache.

I'm using the NSURLRequestReturnCacheDataElseLoad policy, and the cache control headers on the http server are set so everything can be cached.

So the problem is that all requests are made again every time the app is start up. Why isn't this coming from disk?

Edit: it seems this problem only exists when there's a query in the url. For example, when I request http://www.apple.com/, everything is fine, on a fresh app start, no request is actually made. When I change to http://www.apple.com/?test, then the first request is always made.

Edit 2: looks like TTURLRequest (part of three20) and SDURLCache are the solution.

Was it helpful?

Solution

Answering my own question:

The solution is to add SDURLCache to your project and then add this code to your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method:

SDURLCache *URLCache = [[SDURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:24 * 1024 * 1024 diskPath:[SDURLCache defaultCachePath]];
[NSURLCache setSharedURLCache:URLCache];

This is a lot more aggressive with caching, including urls with queries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top