<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codec &#187; NSURLRequest</title>
	<atom:link href="http://www.trembl.org/codec/tag/nsurlrequest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trembl.org/codec</link>
	<description>A Personal Polylogic Code/Decode &#039;Zettelkasten&#039;</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:38:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Caching on Objective-C with NSURLCache</title>
		<link>http://www.trembl.org/codec/269/</link>
		<comments>http://www.trembl.org/codec/269/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 07:43:05 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[cachePolicy cache]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[NSURLRequest]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[request]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=269</guid>
		<description><![CDATA[A quick reminder on how caching works in Objective-C. Initially there is one cache, NSURLCache administrates it. Get the object: NSURLCache *cache = &#91;NSURLCache sharedURLCache&#93; &#160; &#91;cache memoryCapacity&#93;; &#91;cache setMemoryCapacity: 1024*1024*1&#93;; // in byte &#91;cache currentMemoryUsage&#93;; Don&#8217;t worry about the disk aka the flash memory in iPhone, it&#8217;s not accessible. A bit of strange behaviour, [...]]]></description>
			<content:encoded><![CDATA[<p>A quick reminder on how caching works in Objective-C.</p>
<p>Initially there is one cache, NSURLCache administrates it.</p>
<p>Get the object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLCache</span> <span style="color: #002200;">*</span>cache <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLCache</span> sharedURLCache<span style="color: #002200;">&#93;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>cache memoryCapacity<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>cache setMemoryCapacity<span style="color: #002200;">:</span> <span style="color: #2400d9;">1024</span><span style="color: #002200;">*</span><span style="color: #2400d9;">1024</span><span style="color: #002200;">*</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// in byte</span>
<span style="color: #002200;">&#91;</span>cache currentMemoryUsage<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Don&#8217;t worry about the disk <em>aka the flash memory</em> in iPhone, it&#8217;s not accessible.</p>
<p>A bit of strange behaviour, either a bug in the software or in my brain:<br />
When changing ViewControllers the memoryCapacity seems to reset itself to 0, although previous caches remain intact. Re-setting the memoryCapacity back to the original level seems to solve this. (The cache is not overwritten.)</p>
<p>Using the Cache<br />
The whole point of using the cache is to not download online material over again. The following steps make for a happy cache:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//URLRequest</span>
<span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>theURL
cachePolicy<span style="color: #002200;">:</span>NSURLRequestUseProtocolCachePolicy timeoutInterval<span style="color: #002200;">:</span>10.0f<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The delegate response would allow you to alter the about-to-be-cached-data, leaving it in is not necessary:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>c
willCacheResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;willCacheResponse&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> response;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>All in all, not that much to take it. Finding out, that the memoryCapacity gets reset was the most time-consuming bit.<br />
For another example, of how to roll your own cache, look at <a href="http://developer.apple.com/iphone/library/samplecode/URLCache/index.html">URLCache</a> example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/269/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

