<?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>Just2us &#187; CoreData</title>
	<atom:link href="http://just2us.com/tag/coredata/feed/" rel="self" type="application/rss+xml" />
	<link>http://just2us.com</link>
	<description>Its about us</description>
	<lastBuildDate>Sat, 31 Jul 2010 16:03:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Pitfalls of NSFetchedResultsController</title>
		<link>http://just2us.com/2010/02/pitfalls-of-nsfetchedresultscontroller/</link>
		<comments>http://just2us.com/2010/02/pitfalls-of-nsfetchedresultscontroller/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:06:59 +0000</pubDate>
		<dc:creator>samwize</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://just2us.com/2010/02/pitfalls-of-nsfetchedresultscontroller/</guid>
		<description><![CDATA[I have recently used one of the most useful framework in iPhone 3.0 – CoreData. There are many guides on using CoreData, such as from cocoadevcentral or Apple’s guide. But what I found lacking is that there was no discussion on the pitfalls of using CoreData, or its view controller, NSFetchedResultsController. I learnt the pitfalls [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjust2us.com%2F2010%2F02%2Fpitfalls-of-nsfetchedresultscontroller%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjust2us.com%2F2010%2F02%2Fpitfalls-of-nsfetchedresultscontroller%2F&amp;source=samwize&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I have recently used one of the most useful framework in iPhone 3.0 – CoreData. </p>
<p>There are many guides on using CoreData, such as from <a href="http://cocoadevcentral.com/articles/000086.php">cocoadevcentral</a> or <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-TP1">Apple’s guide</a>. But what I found lacking is that there was no discussion on the pitfalls of using CoreData, or its view controller, <a href="http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html">NSFetchedResultsController</a>.</p>
<p>I learnt the pitfalls along the way, and there are 3 particular pitfalls that I would like to share with developers while developing <a href="http://txeet.com">txeet</a> for iPhone.</p>
<p>&#160;</p>
<h2>1. Performance hit with predicate using one-to-many relationship</h2>
<p>When using NSFetchedResultsController, we would often form our predicate (like SQL) to retrieve some table rows. </p>
<p>If you were to use a predicate that involves transversing a one-to-many relationship, the performance could be slowed down <a href="http://stackoverflow.com/questions/1145178/core-data-fetching-objects-that-are-in-relationship-to-another-object">tremendously</a> (as slow as 30 sec to run, or even crash!). Take for example a <strong>Template</strong> model that has a one-to-many relationship <strong>tags</strong> to <strong>Tag</strong> model:</p>
<p><a href="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode1.jpg"><img title="coredata pitfall code1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="40" alt="coredata pitfall code1" src="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode1_thumb.jpg" width="624" border="0" /></a>&#160; </p>
<p>The predicate above would require transversing to each Tag to find ‘love’. This is computationally <em>very expensive</em>. </p>
<p>The solution to this is to avoid transversing relationship. A faster way that CoreData could execute is to access the properties/attributes. For the above example, what I did is to add another attribute <strong>tagsAsAttribute</strong> to <strong>Template</strong> model. This property would store the tag names in a delimited format such as “;love;jokes;quotes;”. The predicate would then be changed to:</p>
<p><a href="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode2.jpg"><img title="coredata pitfall code2" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="31" alt="coredata pitfall code2" src="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode2_thumb.jpg" width="679" border="0" /></a> </p>
<p>Note: This is not the best way to design the data model, as <strong>tagsAsAttribute</strong> has a dependency and is redundant.</p>
<p>&#160;</p>
<h2>2. User-driven updates</h2>
<p>If you read the <a href="http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference.html">Apple’s guide to NSFetchedResultsControllerDelegate</a>, please note of the section User-Driven Updates.</p>
<p>In short, if you have user-driven updates, you should write the following as the first line in every (only one is shown below) of the NSFetchedResultsControllerDelegate delegate methods:</p>
<p><a href="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode3.jpg"><img title="coredata pitfall code3" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="37" alt="coredata pitfall code3" src="http://just2us.com/site/wp-content/uploads/2010/02/coredatapitfallcode3_thumb.jpg" width="557" border="0" /></a> </p>
<p>And when there is a user-driven update, set the <strong>isStillUpdating</strong> boolean to true, and set false when the update is completed.</p>
<p>User-driven updates could be re-ordering of table rows, or inserting of objects.</p>
<p>&#160;</p>
<h2>3. abort() should NEVER be used in release</h2>
<p>If you have used the sample code for CodeData, there is a line of code which is there only for testing environment. In the comments, it warned developers that <strong>abort()</strong> should not be used in shipping application..</p>
<p>But I didn’t read the comments..</p>
<p>Apparently, certain devices will occasionally have error when running certain CoreData methods. Eg. NSManagedObjectContext’s save. When there is an error, we could optionally handle the error, but we should NEVER abort and exit the app.</p>
]]></content:encoded>
			<wfw:commentRss>http://just2us.com/2010/02/pitfalls-of-nsfetchedresultscontroller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
