Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLConnection on iOS doesn't try to cache objects larger than 50KB

Despite Apple's documentation indicating otherwise, NSURLCache on iOS doesn't do any disk (flash) caching at all. You can subclass NSURLCache to change the behaviour of the fetch and store operations to use the disk (like SDURLCache does), but due to the following severe limitations of how the cache is used and implemented, this doesn't work as well as you'd expect:

  • NSURLConnection doesn't even call storeCachedResponse:forRequest: for files over about 50KB (>= 52428 bytes, to be exact). This makes subclassing NSURLCache pointless for our use (200KB images), because it won't even get to the cache. As a result, we have to add caching manually at a level above NSURLConnection.
  • Even when one calls the NSURLCache's built-in storeCachedResponse:forRequest: manually, it only stores the response in memory if it's less than about 180KB. I tested this by calling storeCachedResponse manually and seeing that the before/after currentMemoryUsage didn't change for data lengths above about 180KB. So we have to write our own LRU memory caching too.

Has anyone else noticed these issues? Or is there something I'm missing?

FYI, I'm running iOS 4.3 in the simulator and on an iPad 2.

like image 385
Ben Hoyt Avatar asked Aug 23 '11 19:08

Ben Hoyt


1 Answers

I'd suggest using ASIHTTPRequest library instead of NSURLRequest:

http://allseeing-i.com/ASIHTTPRequest/How-to-use

It has a robust caching API:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache

like image 160
Vibhor Goyal Avatar answered Oct 07 '22 01:10

Vibhor Goyal