<?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>iPhone Programming Tutorials &#187; nsoperation</title>
	<atom:link href="/tag/nsoperation/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Cloning UIImagePickerController using the Assets Library Framework</title>
		<link>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/</link>
		<comments>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 22:51:05 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Custom UITableViewCells]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[nsoperation]]></category>
		<category><![CDATA[UIGestureRecognizers]]></category>
		<category><![CDATA[UIImagePickerController]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2338</guid>
		<description><![CDATA[Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow  ...]]></description>
				<content:encoded><![CDATA[<p>Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow users to select multiple photos from their photo album for a slideshow. While the iPhone photos application allows for this, the UIImagePicker does not. With the release of the Assets Library framework we can now recreate the experience of the UIImagePicker, with some additional custom functionality to fit our needs. As a result we created a new cloned version of the UIImagePicker that allows for multiple photo selection just like the photos app. We have decided to open source the controller for other developers to use. This post will explain the process of creating the new image picker as well as the method of incorporating it into your code.</p>
<h2>The ELCImagePickerController</h2>
<p>The ELCImagePickerController is only possible because of the newly introduced Assets Library framework. This framework gives you raw (more or less) access to the photo and video elements of a user. We looked at UIImagePickerController and saw a lot of weaknesses with it. You can only select 1 photo at a time, and even in Apple&#8217;s photo app, where you can choose several pictures at a time, you can&#8217;t use multi touch to do your selection. To solve these problems we rolled our own solution that works very closely to UIImagePickerController.</p>
<h3>How to use it</h3>
<p>First I am going to explain using the picker since to many people the process of creating it won&#8217;t be very interesting. The image picker is created and displayed in a very similar manner to the UIImagePickerController. The sample application that is part of the GitHub project, where I distribute the controller, shows its use, but I will go into detail here. To display the controller you instantiate it and display it modally like so.</p>
<pre>ELCImagePickerController *controller = [[ELCImagePickerController alloc] initImagePicker];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];</pre>
<p>The ELCImagePickerController will return the select images back to the ELCImagePickerControllerDelegate. The delegate contains to methods very similar to the UIImagePickerControllerDelegate. Instead of returning one dictionary representing a single image the controller sends back an array of similarly structured dictionaries. The two delegate methods are:]</p>

<div class="wp_syntax"><table><tr><td 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>elcImagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingMediaWithInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>info;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>elcImagePickerControllerDidCancel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker;</pre></td></tr></table></div>

<h2>GIT Hub</h2>
<p>You can find this project <a title="ELCImagePickerController on GITHub" href="http://github.com/elc/ELCImagePickerController" target="_blank">now on GitHub</a>. Please let me know any issues you may have and look for future releases with feature enhancements</p>
<h2>General Structure</h2>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png"><img class="aligncenter size-full wp-image-2444" title="ELCImagePickerControllerArchitecture" src="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png" alt="" width="526" height="306" /></a></p>
<p style="text-align: left;">The ELCImagePicker is a collection of UITableViewControllers that are placed inside a UINavigationController. While the ELCImagePickerController is actually 5 separate custom classes I have written, I have put them all within a single header and main. I chose this to make the classes that were required to be imported into a project when using this as few as possible.  While usually when presenting a UINavigationController you would create one yourself in code and use the initWithRootViewController method, in this case we have created a UINavigationController subclass called ELCImagePickerController which does all this behind the scenes. In the end all a developer has to do is use the initImagePicker method and present the controller modally. This lets the class match the functionality of UIImagePickerController closer. You can see the Header and Main for the ELCImagePickerController class on the next page.</p>
<p><a href="http://vimeo.com/15666311">ELCImagePickerControllerDemo</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>iPhone Coding &#8211; Turbo Charging Your Apps With NSOperation</title>
		<link>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/</link>
		<comments>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 19:28:18 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[nosoperationqueue]]></category>
		<category><![CDATA[nsinvocationoperation]]></category>
		<category><![CDATA[nsoperation]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1792</guid>
		<description><![CDATA[<p>So, let's face it, MANY applications in the app store are "Clunky".  They have jittery interfaces, poor scrolling performance, and the UI tends to lock up at times.  The reason? DOING ANYTHING OTHER THAN INTERFACE MANIPULATION IN THE MAIN APPLICATION THREAD!</p>

<p>What do I mean by this? Well, I am essentially talking about multithreading your application.  If you don't know what is meant by multithreading, I suggest you read up on it and return to this post.  Let's dig in and I'll give you an example of the problem.</p>]]></description>
				<content:encoded><![CDATA[<h4>Introduction</h4>
<p>So, let&#8217;s face it, MANY applications in the app store are &#8220;Clunky&#8221;.  They have jittery interfaces, poor scrolling performance, and the UI tends to lock up at times.  The reason? DOING ANYTHING OTHER THAN INTERFACE MANIPULATION IN THE MAIN APPLICATION THREAD!</p>
<p>What do I mean by this? Well, I am essentially talking about multithreading your application.  If you don&#8217;t know what is meant by multithreading, I suggest you <a href="http://en.wikipedia.org/wiki/Multithreading">read up on it</a> and return to this post OR don&#8217;t worry about it because you don&#8217;t need much threading knowledge for this tutorial.  Let&#8217;s dig in and I&#8217;ll give you an example of the problem.</p>
<h4>The Problem</h4>
<p>When you create an application, the iPhone spawns a new process containing the main thread of your application.  All of interface components are run inside of this thread (table views, tab bars, alerts, etc&#8230;).  At some point in your application, you will want to populate these views with data.  This data can be retrieved from the disk, the web, a database, etc&#8230; The problem is: How do you efficiently load this data into your interface while still allowing the user to have control of the application.</p>
<p>Many applications in the store simply &#8216;freeze&#8217; while their application data is being loaded.  This could be anywhere from a tenth of a second to much longer. Even the smallest amount of time is noticeable to the user.</p>
<p>Now, don&#8217;t get me wrong, I am not talking about applications that display  a loading message on the screen while the data populates.  In most cases, this is acceptable, but can not be done effectively unless the data is loaded in another thread besides the main one.</p>
<p>Here is a look at the application we will be creating today:</p>
<p><img class="alignnone size-full wp-image-1806" title="Screen shot 2010-03-04 at 12.15.47 PM" src="/wp-content/uploads/2010/03/Screen-shot-2010-03-04-at-12.15.47-PM1.png" alt="" width="414" height="770" /></p>
<p>Let&#8217;s take a look at the incorrect way to load data into a UITableView from data loaded from the web.   The example below reads a plist file from icodeblog.com containing 10,000 entries and populates a UITableView with those entries.  This happens when the user presses the &#8220;Load&#8221; button.</p>
<h3><span style="color: #ff0000;">Wrong (<a href="/wp-content/uploads/2010/03/NSOperationTableWRONG1.zip"><span style="color: #ff0000;">download this code here to see for yourself</span></a>)</span></h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> RootViewController
<span style="color: #a61390;">@synthesize</span> array;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Adding the button */</span>
    self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Load&quot;</span>
        style<span style="color: #002200;">:</span>UIBarButtonItemStyleDone
        target<span style="color: #002200;">:</span>self
        action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadData<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Initialize our array */</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">10000</span><span style="color: #002200;">&#93;</span>;
    self.array <span style="color: #002200;">=</span> _array;
    <span style="color: #002200;">&#91;</span>_array release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Fires when the user presses the load button</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> loadData <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Grab web data */</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>dataURL <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://icodeblog.com/samples/nsoperation/data.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tmp_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfURL<span style="color: #002200;">:</span>dataURL<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Populate our array with the web data */</span>
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> tmp_array<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.array addObject<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* reload the table */</span>
    <span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Table view methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.array count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault
                reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Display the text of the array */</span>
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.array objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<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>super dealloc<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>array release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>&#8220;Looks good to me&#8221;, you may say.  But that is incorrect.  If you run the code above, pressing the &#8220;Load&#8221; button will result in the interface &#8216;freezing&#8217; while the data is being retrieved from the web.  During that time, the user is unable to scroll or do anything since the main thread is off downloading data.</p>
<h4>About NSOperationQueue And NSOperation</h4>
<p>Before I show you the solution, I though I would bring you up to speed on NSOperation.</p>
<p>According to Apple&#8230;</p>
<blockquote><p>The <code>NSOperation</code> and <code>NSOperationQueue</code> classes alleviate much of the pain of multi-threading, allowing you to simply define your tasks, set any dependencies that exist, and fire them off. Each task, or <strong>operation</strong>, is represented by an instance of an <code>NSOperation</code> class; the <code>NSOperationQueue</code> class takes care of starting the operations, ensuring that they are run in the appropriate order, and accounting for any priorities that have been set.</p></blockquote>
<p>The way it works is, you create a new NSOperationQueue and add NSOperations to it.  The NSOperationQueue creates a new thread for each operation and runs them in the order they are added (or a specified order (advanced)).  It takes care of all of the autorelease pools and other garbage that gets confusing when doing multithreading and greatly simplifies the process.</p>
<p>Here is the process for using the NSOperationQueue.</p>
<ol>
<li>Instantiate a new NSOperationQueue object</li>
<li>Create an instance of your NSOperation</li>
<li>Add your operation to the queue</li>
<li>Release your operation</li>
</ol>
<p>There are a few ways to work with NSOperations.  Today, I will show you the simplest one: NSInvocationOperation.  NSInvocationOperation is a subclass of NSOperation which allows you to specify a target and selector that will run as an operation.</p>
<p>Here is an example of how to execute an NSInvocationOperation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSOperationQueue</span> <span style="color: #002200;">*</span>queue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSOperationQueue</span> new<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSInvocationOperation</span> <span style="color: #002200;">*</span>operation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocationOperation</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self
    selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>methodToCall<span style="color: #002200;">&#41;</span>
    object<span style="color: #002200;">:</span>objectToPassToMethod<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>queue addOperation<span style="color: #002200;">:</span>operation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>operation release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This will call the method &#8220;methodToCall&#8221; passing in the object &#8220;objectToPassToMethod&#8221; in a separate thread.  Let&#8217;s see how this can be added to our code above to make it run smoother.</p>
<h4>The Solution</h4>
<p>Here we still have a method being fired when the user presses the &#8220;Load&#8221; button, but instead of fetching the data, this method fires off an NSOperation to fetch the data.  Check out the updated code.</p>
<h3><span style="color: #00ff00;">Correct (<span style="color: #00ff00;"><a href="/wp-content/uploads/2010/03/NSOperationTable11.zip"><span style="color: #00ff00;">Download the source code here</span></a></span>)</span></h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> RootViewController
<span style="color: #a61390;">@synthesize</span> array;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Load&quot;</span>
       style<span style="color: #002200;">:</span>UIBarButtonItemStyleDone
       target<span style="color: #002200;">:</span>self
       action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadData<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">10000</span><span style="color: #002200;">&#93;</span>;
    self.array <span style="color: #002200;">=</span> _array;
    <span style="color: #002200;">&#91;</span>_array release<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> loadData <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Operation Queue init (autorelease) */</span>
    <span style="color: #400080;">NSOperationQueue</span> <span style="color: #002200;">*</span>queue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSOperationQueue</span> new<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */</span>
    <span style="color: #400080;">NSInvocationOperation</span> <span style="color: #002200;">*</span>operation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocationOperation</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadDataWithOperation<span style="color: #002200;">&#41;</span>
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Add the operation to the queue */</span>
    <span style="color: #002200;">&#91;</span>queue addOperation<span style="color: #002200;">:</span>operation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>operation release<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> loadDataWithOperation <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>dataURL <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://icodeblog.com/samples/nsoperation/data.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tmp_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfURL<span style="color: #002200;">:</span>dataURL<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> tmp_array<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.array addObject<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self.tableView performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>reloadData<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Table view methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.array count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.array objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<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>super dealloc<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>array release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, we haven&#8217;t added much code here, but we have GREATLY improved the overall user experience.  So, what did I do exactly?</p>
<ol>
<li>Moved all of the processing (downloading) code from the loadData method to another method that could be run asynchronously</li>
<li>Created a new instance of NSOperationQueue by calling [NSOperationQueue new]</li>
<li>Created an NSInvocationOperation to call our method loadDataWithOperation</li>
<li>Added the operation to the queue</li>
<li>Released the operation</li>
<li>When the Data has been downloaded, we reload the table data in the main thread since it&#8217;s a UI manipulation</li>
</ol>
<p>One thing to note here is we never actually tell the operation to run.  This is handled automatically in the queue.   The queue will figure out the optimal time run the operation and do it for you.</p>
<p>Now that you have your downloading and processing in a separate thread, you are now free to add things such as a loading view.</p>
<p>I will be expanding on this tutorial in the coming week and showing you how to cache data and display old data to the user while the new is loading.  This is a popular technique used in many Twitter and News applications.</p>
<p>That concludes today&#8217;s tutorial.</p>
<p>Post questions in the comments or <a href="http://twitter.com/brandontreb">Ping me on Twitter</a>.</p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
	</channel>
</rss>
