views:

301

answers:

3

I had considered writing a custom url cache, but it seems that NSURLCache is supposed to support a disk cache. I'd like to have my iphone app use cached responses without hitting the server at all until the resource becomes stale (as determined by the headers I send back from the server).

Do I have to call something fancy with NSURLConnection? How can I get the iphone to use the cached version of the url?

Update: Here is a link to my code

http://pastie.org/808549

+1  A: 

I think you may need to add a "cachePolicy" to your NSURLRequest:

The NSURLRequestReturnCacheDataElseLoad cache policy will cause the URL loading system to use cached data ignoring its age or expiration date, if it exists, and load the data from the originating source only if there is no cached version.

http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Concepts/CachePolicies.html#//apple_ref/doc/uid/20001843

There's a few different policies there. Perhaps one of them will help.

Typeoneerror
It seems like the default policy should work. The problem isn't whether or not it loads from the cache, but that it isn't even storing it in the cache. Thanks. I'll try a couple of those.
Sean Clark Hess
Switching that didn't seem to do anything. It looks like I may have to write my own :(
Sean Clark Hess
Just found this sample code while I was looking for something else: http://developer.apple.com/iphone/library/samplecode/URLCache/index.html
Typeoneerror
Yeah, that example does it by hand -- I was hoping to avoid it :) Thanks
Sean Clark Hess
+1  A: 

Take a look at the ASI classes while you're at it.

http://allseeing-i.com/ASIHTTPRequest/

They are pretty nice.

xyzzycoder
A: 

The answer is, unfortunately, that the iPhone can't do disk caching with NSURLCache, making it inappropriate for the scenario I outlined.

I'm rolling my own class.

Sean Clark Hess