<?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>MRo Blog &#187; NSURLCache</title>
	<atom:link href="http://blog.mro.name/tag/nsurlcache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mro.name</link>
	<description>Marcus Rohrmoser mobile Software</description>
	<lastBuildDate>Wed, 23 Jun 2010 11:32:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>NSCachedURLResponse / NSKeyedUnarchiver pain</title>
		<link>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/</link>
		<comments>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 18:42:00 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSCachedURLResponse]]></category>
		<category><![CDATA[NSKeyedArchiver]]></category>
		<category><![CDATA[NSKeyedUnarchiver]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1667</guid>
		<description><![CDATA[as the iPhone SDK comes with a rather dysfunctional NSURLCache — Apple suggests to implement it from scratch yourself in the code examples about caching — I went for just this. Until I came across the [NSKeyedUnarchiver unarchiveObjectWithData:...] not restoring userInfo, storagePolicy and data of NSCachedURLResponse. Couldn&#8217;t believe it and spent almost the whole day verifying [...]]]></description>
			<content:encoded><![CDATA[<p>as the iPhone SDK comes with a rather dysfunctional <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html">NSURLCache</a> — Apple suggests to implement it from scratch yourself in the <a href="http://developer.apple.com/iphone/library/samplecode/URLCache/index.html#//apple_ref/doc/uid/DTS40008061">code examples about caching</a> — I went for just this.</p>
<p>Until I came across the <code>[<a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedUnarchiver_Class/Reference/Reference.html">NSKeyedUnarchiver</a> unarchiveObjectWithData:...]</code> not restoring <code>userInfo</code>, <code>storagePolicy</code> and <code>data</code> of <code><a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSCachedURLResponse_Class/Reference/Reference.html">NSCachedURLResponse</a></code>.</p>
<p>Couldn&#8217;t believe it and spent almost the whole day verifying that the error is not within my code but really the unarchiver treats those fields transient.</p>
<p>See yourself:</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: #a61390;">void</span><span style="color: #002200;">&#41;</span>testiPhoneSDK312NSKeyedArchiver
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//  prepare a NSCachedURLResponse</span>
    <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>src <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>i_url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.url.example/path/file.html&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>i_mime <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text/html&quot;</span>;
        NSInteger expectedContentLength <span style="color: #002200;">=</span> <span style="color: #2400d9;">112</span>;
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>i_encoding <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iso-8859-2&quot;</span>;
&nbsp;
        <span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>i_response <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLResponse</span> alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span>i_url
            MIMEType<span style="color: #002200;">:</span>i_mime
            expectedContentLength<span style="color: #002200;">:</span>expectedContentLength
            textEncodingName<span style="color: #002200;">:</span>i_encoding<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>i_data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello, world!&quot;</span> dataUsingEncoding<span style="color: #002200;">:</span>NSISOLatin2StringEncoding<span style="color: #002200;">&#93;</span>;
        NSURLCacheStoragePolicy i_storage <span style="color: #002200;">=</span>  NSURLCacheStorageAllowed;
        <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>i_userInfo <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span>
        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSince1970<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;era&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>src <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCachedURLResponse</span> alloc<span style="color: #002200;">&#93;</span> initWithResponse<span style="color: #002200;">:</span>i_response
            data<span style="color: #002200;">:</span>i_data
            userInfo<span style="color: #002200;">:</span>i_userInfo
            storagePolicy<span style="color: #002200;">:</span>i_storage<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//      ensure it's all in place:</span>
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_url, src.response.URL, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_mime, src.response.MIMEType, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEquals<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>expectedContentLength, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>src.response.expectedContentLength, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file.html&quot;</span>, src.response.suggestedFilename, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_encoding, src.response.textEncodingName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_data, src.data, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_userInfo, src.userInfo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEquals<span style="color: #002200;">&#40;</span> 0u, src.storagePolicy, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//  archive + unarchive:</span>
    <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>dst <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSKeyedUnarchiver</span> unarchiveObjectWithData<span style="color: #002200;">:</span>
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSKeyedArchiver</span> archivedDataWithRootObject<span style="color: #002200;">:</span>src<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//  check whether src == dst</span>
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.URL, dst.response.URL, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.MIMEType, dst.response.MIMEType, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span>src.response.expectedContentLength, dst.response.expectedContentLength, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.suggestedFilename, dst.response.suggestedFilename, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.textEncodingName, dst.response.textEncodingName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//  !!!!!!!!!!</span>
<span style="color: #11740a; font-style: italic;">//  sad information loss after unarchiving:</span>
    STAssertNil<span style="color: #002200;">&#40;</span> dst.data, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span> dst.userInfo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span> 2u , dst.storagePolicy, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When my NSURLCache replacement is ready I think about publishing it a github — interested anyone?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSURLCache Joke / iPhone</title>
		<link>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/</link>
		<comments>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 12:08:18 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/2009/07/nsurlcache-joke-iphone/</guid>
		<description><![CDATA[Did you ever wonder why Apple&#8217;s own Demo App URLCache doesn&#8217;t use the NSURLCache class, but rather reimplements disk caching instead? Well, it looks like NSURLCache promises disk-caching, but doesn&#8217;t keep this promise.]]></description>
			<content:encoded><![CDATA[<p>Did you ever wonder why Apple&#8217;s own Demo App <a href="https://developer.apple.com/iphone/library/samplecode/URLCache/index.html">URLCache</a> doesn&#8217;t use the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html">NSURLCache</a> class, but rather reimplements disk caching instead? Well, it looks like NSURLCache promises disk-caching, but doesn&#8217;t keep this promise.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
