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:
the request must 1 can cached (e.g.
http
orhttps
, notftp
).the response must generate headers indicate indicate response can cached. notably, must set
cache-control
. see nshipster's discussion onnsurlcache
.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); ?>
the response must satisfy poorly documented rules (e.g. response cannot exceed 5% of total persistent
nsurlcache
). example, can place following in app delegate'sdidfinishlaunchingwithoptions
: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.
for sake of completeness, i'll state obvious, when creating
nsurlrequest
,nsurlrequestcachepolicy
ofnsurlrequestreloadignoringlocalcachedata
prevent cache being used if response cached.in same category of stating obvious,
connection:willcacheresponse:
method returnednil
prevent response being cached.
Comments
Post a Comment