<?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; asynchronous</title>
	<atom:link href="http://blog.emmerinc.be/index.php/tag/asynchronous/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>Wed, 23 Jun 2010 20:57:28 +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>16</slash:comments>
		</item>
	</channel>
</rss>
