<?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>Mac / iPhone App Development &#187; iPhone</title>
	<atom:link href="http://benreeves.co.uk/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://benreeves.co.uk</link>
	<description>Home of a Small Time Developer</description>
	<lastBuildDate>Wed, 26 May 2010 14:24:12 +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>iPhone Expat XML Parser Wrapper</title>
		<link>http://benreeves.co.uk/iphone-expat-xml-parser-wrapper/</link>
		<comments>http://benreeves.co.uk/iphone-expat-xml-parser-wrapper/#comments</comments>
		<pubDate>Mon, 24 May 2010 14:30:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Atom]]></category>
		<category><![CDATA[Expat]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSXMLParser]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://benreeves.co.uk/?p=314</guid>
		<description><![CDATA[Often in iPhone projects I&#8217;ve needed to parse XML documents from the internet.

I want the data to be processed  and shown to the user as quickly as possible.
If the file is large I don&#8217;t want to have to store the entire contents in memory.

Using NSXMLParsers initWithURL: method the xml file is downloaded synchronously with NSURLConnection before parsing. If [...]]]></description>
			<content:encoded><![CDATA[<p>Often in iPhone projects I&#8217;ve needed to parse XML documents from the internet.</p>
<ul>
<li>I want the data to be processed  and shown to the user as quickly as possible.</li>
<li>If the file is large I don&#8217;t want to have to store the entire contents in memory.</li>
</ul>
<p>Using NSXMLParsers initWithURL: method the xml file is downloaded synchronously with NSURLConnection before parsing. If NSXMLParser was able to begin as soon as stream buffer began to fill, even though the overall parsing time wouldn&#8217;t be reduced significantly if the updates are immediately displayed the user perceived time decreases significantly.</p>
<p>Using the excellent <a href="http://www.robbiehanson.com/expat.html">objective c expat wrapper</a> library by <a href="http://www.robbiehanson.com/">Robbie Hanson</a> as a base iPhoneExpat uses CFNetwork &amp; CFHTTPMessage to create and feed a http stream gradually into Expat. If the server supports gzip compression <a href="http://www.zlib.net/">zlib</a> is used to decompress the stream on fly.</p>
<p>iPhone Expat offer 3 methods for initialization</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;">id</span><span style="color: #002200;">&#41;</span>initWithContentsOfURL<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;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithContentsOfFile<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>path;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithData<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;</pre></div></div>

<p>The delegate messages are also a drop in replacement for NSXMLParser</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> ExpatXMLParserDelegate 
&nbsp;
@optional
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parserDidStartDocument<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parserDidEndDocument<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didStartMappingPrefix<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>prefix toURI<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>namespaceURI;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didEndMappingPrefix<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>prefix;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundComment<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>comment;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundProcessingInstructionWithTarget<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>target data<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>data;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser parseErrorOccurred<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parseError;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser shouldProcessAttributesForElement<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>elementName;
&nbsp;
@required
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didStartElement<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>elementName namespaceURI<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>namespaceURI qualifiedName<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>qualifiedName attributes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>attributeDict;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser didEndElement<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>elementName namespaceURI<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>namespaceURI qualifiedName<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>qName;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundCharacters<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><span style="color: #a61390;">string</span>;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>However</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>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ExpatXMLParser <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundCharacters<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><span style="color: #a61390;">string</span>;</pre></div></div>

<p>Will report the entire contents of a tag so you don&#8217;t need an NSMutablestring to buffer fragments in your delegate. e.g.</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>parser<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parser foundCharacters<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><span style="color: #a61390;">string</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>buffer release<span style="color: #002200;">&#93;</span>;
	buffer <span style="color: #002200;">=</span> <span style="color: #a61390;">string</span>;
	<span style="color: #002200;">&#91;</span>buffer retain<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Should be good in 99% of cases to get the entire contents of a tag</p>
<p><strong>The project can be found here</strong></p>
<p><a href="http://github.com/zootreeves/iPhoneExpat">http://github.com/zootreeves/iPhoneExpat</a></p>
<p>Alternatively checkout the code using git: http://github.com/zootreeves/iPhoneExpat</p>
<p><strong>Benchmark test output:</strong></p>
<p><strong><span style="font-weight: normal;">I&#8217;ve included a test application which sequentially downloads and parses the following feeds:</span></strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topalbums/sf=143441/limit=300/explicit=true/xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topalbums/sf=143441/limit=300/explicit=true/xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://feeds.feedburner.com/DilbertDailyStrip&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://designsponge.blogspot.com/atom.xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.slate.com/rss/&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://rssfeeds.usatoday.com/UsatodaycomBooks-TopStories&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://googleblog.blogspot.com/atom.xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hhttp://api.flickr.com/services/feeds/groups_pool.gne?id=61057342@N00〈=en-us&amp;amp;format=rss_200&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://phobos.apple.com/WebObjects/MZStore.woa/wpa/MRSS/topsongs/limit=25/rss.xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.readwriteweb.com/rss.xml&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://rssfeeds.usatoday.com/UsatodaycomNation-TopStories&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://dictionary.reference.com/wordoftheday/wotd.rss&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.quotationspage.com/data/qotd.rss&quot;</span>,
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://sports.espn.go.com/espn/rss/news&quot;</span></pre></div></div>

<p><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">Time to reach the first element Expat: 8.611217 &#8212; NSXMLParser: 15.404100<br />
Total time for Expat: 10.902272 &#8212; NSXMLParser: 17.108309</p>
<p>Memory is also reduced by a factor of about x4 (see screenshots)<br />

<a href='http://benreeves.co.uk/iphone-expat-xml-parser-wrapper/screen-shot-2010-05-24-at-15-37-29/' title='NSXMLParser memory usage'><img width="150" height="150" src="http://benreeves.co.uk/wp-content/uploads/2010/05/Screen-shot-2010-05-24-at-15.37.29-150x150.png" class="attachment-thumbnail" alt="" title="NSXMLParser memory usage" /></a>
<a href='http://benreeves.co.uk/iphone-expat-xml-parser-wrapper/screen-shot-2010-05-24-at-15-42-40/' title='Screen shot 2010-05-24 at 15.42.40'><img width="150" height="150" src="http://benreeves.co.uk/wp-content/uploads/2010/05/Screen-shot-2010-05-24-at-15.42.40-150x150.png" class="attachment-thumbnail" alt="" title="Screen shot 2010-05-24 at 15.42.40" /></a>
</p>
<p><strong>Further Considerations</strong></p>
<ul>
<li>It would be great if http requests were persistent, however after setting kCFStreamPropertyHTTPAttemptPersistentConnection to true using <a href="http://www.tuffcode.com/">httpscoop</a> I will still able to intercept &#8220;connection close&#8221; messages between requests to the same server. I think I must be doing something wrong here.</li>
<li>It would be preferable if as many objects as possible were released manually rather than using an autorelease pool. For example the delegate methods that pass an elementName it is common for the client to use this string to determine their current tag, but rare that they actually want to retain it. The code could be altered so these strings are not placed in a pool and are released immediately after the delegate message is sent. Not only can the overhead of collection be avoided but CFStringCreateWithCharactersNoCopy can be used as well.</li>
<li>With UTF8 creating CFStrings with the [NSString stringwithutf8string:] was no problem. However when I configured Expat to use UTF-16 characters internally I found that CFStringCreateWithCharacters would often return garbage. The problem is I was passing in UINT16_MAX as the buffer length hoping that the CFString would be terminated at null, however it doesn&#8217;t work that way and I had to change everything to calculate the buffer size using UniCharStrlen(buffer) before hand.</li>
<li> FTP (CFFTPStream) and file streams could easily be supported.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/iphone-expat-xml-parser-wrapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreading with core Data</title>
		<link>http://benreeves.co.uk/multithreading-with-core-data/</link>
		<comments>http://benreeves.co.uk/multithreading-with-core-data/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 17:23:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[core data]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[sqllite]]></category>

		<guid isPermaLink="false">http://benreeves.co.uk/?p=188</guid>
		<description><![CDATA[Working with core data in a multi threaded environment you have to extremely careful
For example if were creating a news application, we want to be able to import news in the background and display updates to the user with no interruption of the GUI. The task can be divided into several stages:

Download the xml feed [...]]]></description>
			<content:encoded><![CDATA[<p>Working with core data in a multi threaded environment you have to extremely careful</p>
<p>For example if were creating a news application, we want to be able to import news in the background and display updates to the user with no interruption of the GUI. The task can be divided into several stages:</p>
<ol>
<li>Download the xml feed with the latest news</li>
<li>Parse the feed</li>
<li>Insert the articles into core data</li>
<li>Notify the UI and refresh the display.</li>
</ol>
<p>We already have our core data API defined. A simple method to retrieve all the articles and a method to add a new article.<br />
DataSource.h</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DataSource <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span> <span style="color: #002200;">&#125;</span>
<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>getNewsFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addArticle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Article<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>article forRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region;
<span style="color: #a61390;">@end</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now suppose we want to the web service to import the latest articles. We might define our implementation something like this:<br />
WebService.h</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> WebService <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">id</span> delegate;
	LocalDataSource <span style="color: #002200;">*</span> source;
<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: #a61390;">id</span> delegate;
<span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> LocalDataSource <span style="color: #002200;">*</span> source;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>importNewsForRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Our implementation of importNewsForRegion would look something like this:</p>
<p>WebService.m</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>importNewsForRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//Perform in background if called from main</span>
	 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> currentThread<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> mainThread<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>importNewsForRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>region<span style="color: #002200;">&#93;</span>;
		 <span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Setup pool</span>
	 <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	 <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> articles <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>XML parseArticleFeed<span style="color: #002200;">:</span>region.feedURL<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Not safe 1.</span>
	 <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>Article <span style="color: #002200;">*</span> article <span style="color: #a61390;">in</span> articles<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>source addArticle<span style="color: #002200;">:</span>article forRegion<span style="color: #002200;">:</span>region<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Not safe 2.</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>delegate finishedImport<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Not safe 3.</span>
	<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>However this because importNewsForRegion is running in a separate thread this is unsafe and will likely cause core data to crash or corrupt our database.</p>
<ol>
<li>The region passed to to importNews has likely come from a core data context which was constructed on the main thread. Even though were not even altering the region directly with core data it&#8217;s not safe to even read the feedURL, because every operation can trigger faulting.</li>
<li>When we instruct the source to add an article it will use the main threads context, this is fine when we are in the main thread, but in a background thread we need to specify our own context.</li>
<li>Not related to core data, but when we notify the delegate it will likely perform UI updates which are not safe to perform in a background thread. we need to ensure the delegate only received notifications on the main thread.</li>
</ol>
<p>To make our program thread safe we first need to make some small alterations to our API.</p>
<p>DataSource.h</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DataSource <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span> <span style="color: #002200;">&#125;</span>
<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>getNewsFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region;
<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>getNewsFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectContext</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addArticle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Article<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>article forRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>addArticle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Article<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>article forRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectContext</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context;
<span style="color: #a61390;">@end</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>In our data source we specify two more methods which allow us to provide a context for the method to use. All our queries should be updated to only use the provided context. The original methods that don&#8217;t specify a context should call the newly implemented method with the default context, e.g.</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>addArticle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Article<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>article forRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>region
<span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self addArticle<span style="color: #002200;">:</span>article forRegion<span style="color: #002200;">:</span>region context<span style="color: #002200;">:</span>mainThreadContext<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Only when we are using core data in a background thread do we need to use the newly added methods. So we can now update our WebService.m:</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>importNewsForRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>unsafeRegion
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//Perform in background if called from main</span>
	 <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> currentThread<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> mainThread<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>importNewsForRegion<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span> unsafeRegion<span style="color: #002200;">&#93;</span>;
		 <span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Setup pool</span>
	 <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Create a new context just for this thread</span>
	<span style="color: #400080;">NSManagedObjectContext</span> <span style="color: #002200;">*</span> managedObjectContext <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSManagedObjectContext</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>managedObjectContext setPersistentStoreCoordinator<span style="color: #002200;">:</span>  <span style="color: #002200;">&#91;</span>source persistentStoreCoordinator<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">//Create a new region which is safe to use in this thread</span>
	Region <span style="color: #002200;">*</span> region <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Region<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>managedObjectContext existingObjectWithID<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>unsafeRegion objectID<span style="color: #002200;">&#93;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	 <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> articles <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>XML parseArticleFeed<span style="color: #002200;">:</span>region.feedURL<span style="color: #002200;">&#93;</span>;
	 <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>Article <span style="color: #002200;">*</span> article <span style="color: #a61390;">in</span> articles<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
               <span style="color: #11740a; font-style: italic;">//Ensure we provide the context for the source to use</span>
		<span style="color: #002200;">&#91;</span>source addArticle<span style="color: #002200;">:</span>article forRegion<span style="color: #002200;">:</span>region context<span style="color: #002200;">:</span>managedObjectContext<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Save changes and close the new context</span>
	<span style="color: #002200;">&#91;</span>managedObjectContext save<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>managedObjectContext release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>delegate performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>finishedImport<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;">NO</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As you can see we crate a new context using out shared persistentStoreCoordinator, we ensure that anytime we touch core data from this thread we are using this context. The also means that we have to create a new region which belongs to this context, and we do this with the existingObjectWithID method and using the [unsafeRegion objectID]. Finally we ensure that the delegate receives the notification on the main thread.</p>
<p>for more information about threading in core data see:</p>
<p>http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdMultiThreading.html</p>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/multithreading-with-core-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Reachability Code to your app</title>
		<link>http://benreeves.co.uk/adding-reachability-code-to-your-app/</link>
		<comments>http://benreeves.co.uk/adding-reachability-code-to-your-app/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 19:37:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.benreeves.co.uk/?p=130</guid>
		<description><![CDATA[Ok so iVersion 1.4 was declined due to not loading correctly when their is no network connectivity.
When the device is connected to a cellular network, iVersion does not load its contents.  After the user enters the URL (http://svn.collab.net/repos/svn/trunk/) and taps &#8220;connect,&#8221; an error message is received.
Apparently the error message is not clear enough or something, anyway this [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so iVersion 1.4 was declined due to not loading correctly when their is no network connectivity.</p>
<blockquote><p>When the device is connected to a cellular network, iVersion does not load its contents.  After the user enters the URL (<a href="http://svn.collab.net/repos/svn/trunk/" target="_blank" class="broken_link">http://svn.collab.net/repos/svn/trunk/</a>) and taps &#8220;connect,&#8221; an error message is received.</p></blockquote>
<p>Apparently the error message is not clear enough or something, anyway this is not a rant, this is what apple recommends.</p>
<blockquote><p>Please take a look at the Reachability iPhone program sample which demonstrates the use of the System Configuration Reachability API to detect the absence of WiFi and Wireless Wide Area Network (WWAN) services. Your application can then take appropriate action at the first point where network services are required.</p></blockquote>
<p>The sample code can be found on Apple developer site, however the two file we are interested in are:</p>
<p><strong>Download the Reachability Code:</strong><br />
<a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/Reachability.h">Reachability.h</a></p>
<p><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/Reachability.m">Reachability.m</a></p>
<p><strong>How to use:</strong><br />
Lets say you have a simple UILabel which you want to update with either &#8220;(Offline)&#8221; or &#8220;(Online)&#8221; depending on whether the network is reachable. Your view controller would be defined something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyStatusView <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
	UILabel <span style="color: #002200;">*</span> statusLabel;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> IBOutlet UILabel <span style="color: #002200;">*</span> statusLabel;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>note;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateStatus;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Then we add the following code to the view controller .m file:</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>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
	self.reachability <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Reachability reachabilityWithHostName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;google.com&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>reachability startNotifer<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;kNetworkReachabilityChangedNotification&quot;</span> object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self updateStatus<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>viewWillDisappear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
	self.reachability <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> removeObserver<span style="color: #002200;">:</span>self<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>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self updateStatus<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>updateStatus
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reachability changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>reachability currentReachabilityStatus<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NotReachable<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		statusLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;(Offline)&quot;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		statusLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;(Online)&quot;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I think it&#8217;s fairly self explanatory. Basically when the view appears the reachability class is created and started using startNotifier. Then we hook in our reachabilityChanged: method into the notification centre, so it is automatically called when the network status changes. reachabilityChanged: calls updateStatus which simply changes our label depending on the connectivity.</p>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/adding-reachability-code-to-your-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UIKit Modal Prompt</title>
		<link>http://benreeves.co.uk/uikit-modal-prompt/</link>
		<comments>http://benreeves.co.uk/uikit-modal-prompt/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 19:20:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[modal prompt]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.benreeves.co.uk/?p=121</guid>
		<description><![CDATA[in order to not lock the ui iVersion runs it&#8217;s svn commands in a seperate threads. The promblem arises when one of thses threads needs to prompt for a value e.g. a username / password. Firstly UIKit does not provide a modal alert view and secondly UIKit should only be called from the mainThread.
First we [...]]]></description>
			<content:encoded><![CDATA[<p>in order to not lock the ui iVersion runs it&#8217;s svn commands in a seperate threads. The promblem arises when one of thses threads needs to prompt for a value e.g. a username / password. Firstly UIKit does not provide a modal alert view and secondly UIKit should only be called from the mainThread.</p>
<p>First we create a new class which has a view which will be loaded from a nib, a thread condition to lock the calling thread and two NSString fields to store the result and the string of the button which was clicked.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MessagePrompt <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
UIView <span style="color: #002200;">*</span> view;
<span style="color: #400080;">NSCondition</span> <span style="color: #002200;">*</span> promptCondition;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> clickedButton Text;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> result;
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Our new prompt method can be called from the thread which needs to be locked, it will subsequently call UIKit on the main thread.</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;">id</span><span style="color: #002200;">&#41;</span>newPrompt<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MessagePrompt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>prompt
<span style="color: #002200;">&#123;</span>
&nbsp;
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> currentThread<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> mainThread<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
          <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Should not be called by Main thread<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
          <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
     prompt.promptCondition <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCondition</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>prompt performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>prompt<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>view waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>prompt.promptCondition wait<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> prompt;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This method is called by +(id)NewPrompt and displays a prompt in our window. It also adds targets to each UIButton to receive user actions.</p>
<p style="text-align: center;"><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/screen-shot-2009-10-23-at-15.44.00.png"><img class="size-full wp-image-123  aligncenter" title="Prompt Nib Example" src="http://www.benreeves.co.uk/wp-content/uploads/2010/01/screen-shot-2009-10-23-at-15.44.00.png" alt="" width="380" height="269" /></a></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>prompt<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inView
<span style="color: #002200;">&#123;</span>
     <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> nib <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> loadNibNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyPromptView&quot;</span> owner<span style="color: #002200;">:</span>self options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
     inView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nib objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;	
&nbsp;
     <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> subviews <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.view subviews<span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span> cview <span style="color: #a61390;">in</span> subviews<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
          <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>cview isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIButton class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
          <span style="color: #002200;">&#123;</span>
               UIButton <span style="color: #002200;">*</span> button <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UIButton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cview;
                <span style="color: #002200;">&#91;</span>button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>clicked<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchDown<span style="color: #002200;">&#93;</span>;
          <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>inView.superview addSubview<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This is called when a user clicks a button, it finds the value field and retreives and stores the value in self.message. The prompt condition is then signalled which causes the calling thread to continue running.</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>IBAction<span style="color: #002200;">&#41;</span>clicked<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
     self.clickedButton <span style="color: #002200;">=</span> sender.currentTitle;
&nbsp;
     UITextView <span style="color: #002200;">*</span> text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>iVersionAppDelegate findSubViewOfKind<span style="color: #002200;">:</span>self.view class<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UITextView class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>text<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
         self.message <span style="color: #002200;">=</span> text.text;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self.view removeFromSuperview<span style="color: #002200;">&#93;</span>;
     self.view <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>promptCondition <span style="color: #a61390;">signal</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Usage</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/* Called when svn requires a log message */</span>
svn_error_t <span style="color: #002200;">*</span>
iversion_svn_cl__get_log_message<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">**</span>log_msg,
<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">**</span>tmp_file,
<span style="color: #a61390;">const</span> apr_array_header_t <span style="color: #002200;">*</span>commit_items,
<span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>baton,
apr_pool_t <span style="color: #002200;">*</span>pool<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
CommitLogMessagePrompt <span style="color: #002200;">*</span> prompt <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CommitLogMessagePrompt newPrompt<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Clicked Continue */</span>
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>prompt.clickedButton isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Continue&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">*</span>log_msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>prompt.message UTF8String<span style="color: #002200;">&#93;</span>;
         <span style="color: #a61390;">return</span> SVN_NO_ERROR;
    <span style="color: #002200;">&#125;</span>
&nbsp;
     <span style="color: #a61390;">return</span> svn_error_create<span style="color: #002200;">&#40;</span>SVN_ERR_CANCELLED, <span style="color: #a61390;">NULL</span>, _<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Delete Cancelled&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/uikit-modal-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone File System Monitor</title>
		<link>http://benreeves.co.uk/iphone-file-system-monitor/</link>
		<comments>http://benreeves.co.uk/iphone-file-system-monitor/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 19:04:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[file monitor]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.benreeves.co.uk/?p=111</guid>
		<description><![CDATA[I require simple for system monitoring for a new (undisclosed) iphone project. It doesn&#8217;t appear that there is any equivilant to ionotify for the iphone, so I whipped up this polling file system monitor which meets my basic requirements. If you find this code useful feel free to use / distribute etc.

#include &#34;FileSystemMonitor.h&#34;
&#160;
//Setup the monitor
FileSystemMonitor [...]]]></description>
			<content:encoded><![CDATA[<p>I require simple for system monitoring for a new (undisclosed) iphone project. It doesn&#8217;t appear that there is any equivilant to ionotify for the iphone, so I whipped up this polling file system monitor which meets my basic requirements. If you find this code useful feel free to use / distribute etc.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &quot;FileSystemMonitor.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Setup the monitor</span>
FileSystemMonitor <span style="color: #002200;">*</span> monitor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FileSystemMonitor alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
monitor.delegate <span style="color: #002200;">=</span> self;
<span style="color: #002200;">&#91;</span>monitor monitorPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//...In your delegate class</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>nodeWasCreatedAt<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>path
<span style="color: #002200;">&#123;</span>
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Created %s<span style="color: #2400d9;">\n</span>&quot;</span>, <span style="color: #002200;">&#91;</span>path UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</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>nodeWasDeletedAt<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>path
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Deleted %s<span style="color: #2400d9;">\n</span>&quot;</span>, <span style="color: #002200;">&#91;</span>path UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</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>fileSizeDidchange<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>path bytes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span><span style="color: #002200;">&#41;</span>difference
<span style="color: #002200;">&#123;</span>
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;file Added %ul bytes<span style="color: #2400d9;">\n</span>&quot;</span>, difference<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Notes:</strong></p>
<ul>
<li>Can monitor for Deleted files, Created Files and File Size Changes</li>
<li>On an iPhone 3GS it is able to monitor the entire SVN 1.6 source tree and the iVersion source tree (total 11,050 File/Directories) in an execution time of 12s. Due to the low thread priority and the fact that the majority of execution time is IO it does not cause much/any slow down to the UI. However there is still room for improvement.</li>
</ul>
<p><strong>Download:</strong></p>
<p><strong><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/FileSystemMonitor.h">FileSystemMonitor.h</a></strong></p>
<p><strong><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/FileSystemMonitor.m">FileSystemMonitor.m</a></strong></p>
<p><strong><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/FileMonitorDelegateProtocol.h">FileMonitorDelegateProtocol.h</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/iphone-file-system-monitor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iVersion</title>
		<link>http://benreeves.co.uk/75/</link>
		<comments>http://benreeves.co.uk/75/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 17:06:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[svn client]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=75</guid>
		<description><![CDATA[iVersion is a SVN client for the iphone 3G and ipod touch.
iVersion uses the open source subversion 1.6 client library, written in plain C, with an Objective C wrapper for simpler interfacing with the Objective C UI. Using an objective C Wrapper the client can easily be abstracted to work with other version control protocols. In [...]]]></description>
			<content:encoded><![CDATA[<p><strong>iVersion is a SVN client for the iphone 3G and ipod touch.</strong></p>
<p>iVersion uses the open source <a rel="self" href="http://subversion.tigris.org/">subversion 1.6 </a>client library, written in plain C, with an Objective C wrapper for simpler interfacing with the Objective C UI. Using an objective C Wrapper the client can easily be abstracted to work with other version control protocols. In the future <a rel="self" href="http://git-scm.com/">git</a>, <a rel="self" href="http://www.selenic.com/mercurial/wiki/">mercurial</a>and <a rel="self" href="http://bazaar-vcs.org/en/">bazaar</a> support maybe included.</p>
<h2>Features</h2>
<ul>
<li>Multi-threaded non-locking ui</li>
<li>OpenSSL support for https:// and svn+ssh:// connections</li>
<li>svn commands: list, info, cat,</li>
<li>Browse Directories</li>
<li>View Commit Logs</li>
<li>View file Info</li>
<li>Bookmarks, History &amp; Directory Cache through NSUserDefaults</li>
</ul>
<p><em>** Commit Support<br />
Subversion does not allow commiting of a single file without a working copy of the previous directory. A work around for this has been found and commit support should be available in a future version.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/75/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
