ios - How can I tell if NSURLCache is working? -


i've configured memory , disk cache in app delegate not seem cache being used - seems go network every time. there easy way check if data being cached , retrieved on subsequent requests? setting cache have do? need explicitly check cache calling cachedresponseforrequest each time or that? work on simulator? deployment target ios 6.

thanks.

a couple of observations:

  1. the request must 1 can cached (e.g. http or https, not ftp).

  2. the response must generate headers indicate indicate response can cached. notably, must set cache-control. see nshipster's discussion on nsurlcache.

    for example, when downloading image

    <?php  $filename = "image.jpg";  $lastmodified = filemtime($filename); $etagfile = md5_file($filename);  header('last-modified: ' . gmdate("d, d m y h:i:s", $lastmodified) . ' gmt'); header('etag: "' . $etagfile . '"'); header('content-type: image/jpeg'); header('cache-control: public, max-age=1835400'); header('content-length: ' . filesize($filename));  readfile($filename);  ?> 
  3. the response must satisfy poorly documented rules (e.g. response cannot exceed 5% of total persistent nsurlcache). example, can place following in app delegate's didfinishlaunchingwithoptions:

    nsurlcache *urlcache = [[nsurlcache alloc] initwithmemorycapacity: 5 * 1024 * 1024                                                      diskcapacity:20 * 1024 * 1024 diskpath:nil]; [nsurlcache setsharedurlcache:urlcache]; 

    that sets memory cache of 5mb , persistent cache of 20mb.

  4. for sake of completeness, i'll state obvious, when creating nsurlrequest, nsurlrequestcachepolicy of nsurlrequestreloadignoringlocalcachedata prevent cache being used if response cached.

  5. in same category of stating obvious, connection:willcacheresponse: method returned nil prevent response being cached.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -