<?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>Emmer Inc &#187; NSURLConnection</title>
	<atom:link href="http://blog.emmerinc.be/index.php/tag/nsurlconnection/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.emmerinc.be</link>
	<description>Tales from a .NET developer who's making the jump to the iPhone &#38; App Store wonderland.</description>
	<lastBuildDate>Mon, 06 Sep 2010 19:43:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multiple async NSURLConnections example</title>
		<link>http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/</link>
		<comments>http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 23:23:17 +0000</pubDate>
		<dc:creator>Yannick Compernol</dc:creator>
				<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[NSURLConnection]]></category>

		<guid isPermaLink="false">http://blog.emmerinc.be/?p=187</guid>
		<description><![CDATA[After posting the custom NSURLConnection subclass blog post, I thought I might post an example of how it would be used, since quite a lot of visitors came to the blog searching for the keywords: &#8220;multiple asynchronous NSURLConnection&#8221;. The code itself is pretty straightforward, but might prove handy for those developers setting their first steps [...]]]></description>
			<content:encoded><![CDATA[<p>After posting the custom NSURLConnection subclass <a href="http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/">blog post</a>, I thought I might post an example of how it would be used, since quite a lot of visitors came to the blog searching for the keywords: &#8220;multiple asynchronous NSURLConnection&#8221;. The code itself is pretty straightforward, but might prove handy for those developers setting their first steps in Cocoa Touch.</p>
<p>The basic idea is to use a dictionary which stores the received data per tag, remember that the actual connection cannot be used as the key of a dictionary. Each NSURLConnection methods retrieve the NSMutableData object from the dictionary based on the tag and does it magic with it.</p>
<p>If you don&#8217;t know how to use the NSURLConnection class, take a look at the <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html">documentation</a>. That should get you going in no time.</p>
<p>First of all add a mutable dictionary to the header file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>receivedData;</pre></div></div>

<p>Here&#8217;s the actual code. The load method starts the actual asynchronous calls. The startAsyncLoad:tag and dataForConnection methods are convenience methods to keep the code tidy &amp; readable.</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>startAsyncLoad<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>url tag<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tag <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
   CustomURLConnection <span style="color: #002200;">*</span>connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomURLConnection alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> tag<span style="color: #002200;">:</span>tag<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>connection<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #002200;">&#91;</span>receivedData setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableData</span> data<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>connection.tag<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableData</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dataForConnection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CustomURLConnection<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>receivedData objectForKey<span style="color: #002200;">:</span>connection.tag<span style="color: #002200;">&#93;</span>;
   <span style="color: #a61390;">return</span> data;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>load <span style="color: #002200;">&#123;</span>
   receivedData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url1 <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://blog.emmerinc.be&quot;</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url2 <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.emmerinc.be&quot;</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url3 <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://twitter.com/emmerinc&quot;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
   <span style="color: #002200;">&#91;</span>self startAsyncLoad<span style="color: #002200;">:</span>url1 tag<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tag1&quot;</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>self startAsyncLoad<span style="color: #002200;">:</span>url2 tag<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tag2&quot;</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>self startAsyncLoad<span style="color: #002200;">:</span>url3 tag<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tag3&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</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>connection didReceiveResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>dataForConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self dataForConnection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CustomURLConnection<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>dataForConnection setLength<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</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>connection didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>dataForConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self dataForConnection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CustomURLConnection<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>dataForConnection appendData<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connectionDidFinishLoading<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>connection <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>dataForConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self dataForConnection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CustomURLConnection<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>connection release<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #11740a; font-style: italic;">// Do something with the dataForConnection.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>In the connectionDidFinishLoading: method the tag of the connection should be used to determine what to do with the actual received data. And that is about it!</p>
<p>Don&#8217;t forget to release the dictionary in the deconstructor or after all the asynchronous calls have been completed though.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Custom NSURLConnection class with tag</title>
		<link>http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/</link>
		<comments>http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 21:41:15 +0000</pubDate>
		<dc:creator>Yannick Compernol</dc:creator>
				<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[NSURLConnection]]></category>

		<guid isPermaLink="false">http://blog.emmerinc.be/?p=85</guid>
		<description><![CDATA[A caveat one quickly encounters when using multiple asynchronous NSURLConnections with the same target delegate is that there is no easy way to distinguish one connection from the other, in for instance the connection:didReceiveData: method.
Since using multiple asynchronous connections implies handling the received data chunks yourself, one is likely to create a dictionary which contains [...]]]></description>
			<content:encoded><![CDATA[<p>A caveat one quickly encounters when using multiple asynchronous <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html">NSURLConnections</a> with the same target delegate is that there is no easy way to distinguish one connection from the other, in for instance the connection:didReceiveData: method.</p>
<p>Since using multiple asynchronous connections implies handling the received data chunks yourself, one is likely to create a dictionary which contains a NSMutableData object for each NSURLConnection key. Unfortunately does a <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html">NSDictionary</a> copy its keys and this is not something a NSURLConnection supports. A quick solution to avoid this is to make a subclass with a tag to distinguish each connection. This tag can be used as key in the dictionary.</p>
<p>Interface:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> CustomURLConnection <span style="color: #002200;">:</span> <span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tag;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tag;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>request delegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>delegate startImmediately<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>startImmediately tag<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tag;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> CustomURLConnection
&nbsp;
<span style="color: #a61390;">@synthesize</span> tag;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>request delegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>delegate startImmediately<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>startImmediately tag<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tag <span style="color: #002200;">&#123;</span>
   self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>delegate startImmediately<span style="color: #002200;">:</span>startImmediately<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      self.tag <span style="color: #002200;">=</span> tag;
   <span style="color: #002200;">&#125;</span>
   <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
   <span style="color: #002200;">&#91;</span>tag release<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>As you can see, creating a subclass with a tag property is rather easy and solves the dictionary issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
