<?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>Free Time Studios &#187; Core Data Tips &amp; Tricks</title>
	<atom:link href="http://www.freetimestudios.com/cat/core-data/cd-tips-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.freetimestudios.com</link>
	<description></description>
	<lastBuildDate>Mon, 09 Jan 2012 18:52:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Core Data Tips for iPhone Devs Part 2: Better Error Messages</title>
		<link>http://www.freetimestudios.com/2009/11/13/core-data-tips-for-iphone-devs-part-2-better-error-messages/</link>
		<comments>http://www.freetimestudios.com/2009/11/13/core-data-tips-for-iphone-devs-part-2-better-error-messages/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:13:46 +0000</pubDate>
		<dc:creator>Nathan Eror</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Core Data Tips & Tricks]]></category>

		<guid isPermaLink="false">http://www.freetimestudios.com/?p=204</guid>
		<description><![CDATA[Continuing our series of Core Data tips and tricks, we turn to error handling. All good developers know that error handling is an essential piece of quality software. Unfortunately for Cocoa developers, error handling can be a little cumbersome and verbose on our favorite platform. This is a small price to pay to be able [...]]]></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%2Fwww.freetimestudios.com%2F2009%2F11%2F13%2Fcore-data-tips-for-iphone-devs-part-2-better-error-messages%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.freetimestudios.com%2F2009%2F11%2F13%2Fcore-data-tips-for-iphone-devs-part-2-better-error-messages%2F&amp;source=neror&amp;style=normal&amp;service=bit.ly&amp;service_api=R_1991eb0e663e684f67b85bf3b2ecd1f8&amp;b=2" height="61" width="50" />
			</a>
		</div><div class="document">


<p>Continuing our <a class="reference external" href="http://www.freetimestudios.com/cat/core-data/cd-tips-tricks/">series of Core Data tips and tricks</a>, we turn to error handling. All good developers know that error handling is an essential piece of quality software. Unfortunately for Cocoa developers, error handling can be a little cumbersome and verbose on our favorite platform. This is a small price to pay to be able to work with such outstanding libraries and APIs, but the constant code repetition can eat away at our keyboards. Frustratingly, the errors we work so hard to catch and handle are, sometimes, not useful at first glance. This is definitely the case when saving an <tt class="docutils literal">NSManagedObjectContext</tt>.</p>
<div class="section" id="the-problem">
<h2>The Problem</h2>
<p>Early in development, the data model model changes often, and errors are inevitable. Forget to make a new attribute optional, and the code blows up while saving the context. Because I move pretty fast and change things often, this kind of stuff happens to me all the time. Core Data provides me this error message quite often:</p>
<p><tt class="docutils literal">Domain=NSCocoaErrorDomain Code=1560 UserInfo=0x14f5480 &quot;Operation could not be completed. (Cocoa error <span class="pre">1560.)&quot;</span></tt></p>
<p>For the developer inexperienced with Core Data, this is a very disheartening message. It does not give us any indication of <em>what</em> went wrong or <em>how</em> we should attempt to fix the problem.</p>
</div>
<div class="section" id="the-solution">
<h2>The Solution</h2>
<p>It turns out that, if there is more than one error, Core Data puts an array of <tt class="docutils literal">NSError</tt> objects in the <tt class="docutils literal">userInfo</tt> dictionary with the key <tt class="docutils literal">NSDetailedErrorsKey</tt>. This is not obvious, and it drove me crazy for a while until I turned to <a class="reference external" href="http://stackoverflow.com/&quot;StackOverflow&quot;">Stack Overflow</a> where someone else had already asked my question. There, someone named Charles provided <a class="reference external" href="http://stackoverflow.com/questions/1283960/iphone-core-data-unresolved-error-while-saving/1297157#1297157">this solution</a> with code to get at the &quot;real&quot; error. Overjoyed, I turned the code into a macro, and I use it exclusively to save a context during development. Here is the macro:</p>
<div class="highlight"><pre><span class="cp">#define FT_SAVE_MOC(_ft_moc) \</span>
<span class="cp">do { \</span>
<span class="cp">  NSError* _ft_save_error; \</span>
<span class="cp">  if(![_ft_moc save:&amp;_ft_save_error]) { \</span>
<span class="cp">    NSLog(@&quot;Failed to save to data store: %@&quot;, [_ft_save_error localizedDescription]); \</span>
<span class="cp">    NSArray* _ft_detailedErrors = [[_ft_save_error userInfo] objectForKey:NSDetailedErrorsKey]; \</span>
<span class="cp">    if(_ft_detailedErrors != nil &amp;&amp; [_ft_detailedErrors count] &gt; 0) { \</span>
<span class="cp">      for(NSError* _ft_detailedError in _ft_detailedErrors) { \</span>
<span class="cp">        NSLog(@&quot;DetailedError: %@&quot;, [_ft_detailedError userInfo]); \</span>
<span class="cp">      } \</span>
<span class="cp">    } \</span>
<span class="cp">    else { \</span>
<span class="cp">      NSLog(@&quot;%@&quot;, [_ft_save_error userInfo]); \</span>
<span class="cp">    } \</span>
<span class="cp">  } \</span>
<span class="cp">} while(0);</span>
</pre></div>
<p>Now, whenever I need to save the context, I can do it safely and comfortably like so:</p>
<div class="highlight"><pre><span class="n">FT_SAVE_MOC</span><span class="p">([</span><span class="n">self</span> <span class="n">managedObjectContext</span><span class="p">])</span>
</pre></div>
<p>This macro only prints the error messages, but all I want during development is error messages in the console. This code can easily be moved to a function or method to do more robust error handling in a production scenario, but that would fill an entire post on its own. <img src='http://www.freetimestudios.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.freetimestudios.com/2009/11/13/core-data-tips-for-iphone-devs-part-2-better-error-messages/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Core Data Tips for iPhone Devs Part 1: Command Line Shortcuts</title>
		<link>http://www.freetimestudios.com/2009/11/11/core-data-tips-for-iphone-devs-part-1-command-line-shortcuts/</link>
		<comments>http://www.freetimestudios.com/2009/11/11/core-data-tips-for-iphone-devs-part-1-command-line-shortcuts/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:17:31 +0000</pubDate>
		<dc:creator>Nathan Eror</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Core Data Tips & Tricks]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.freetimestudios.com/?p=133</guid>
		<description><![CDATA[I recently finished migrating the data management backend for all of my games to Core Data from a custom sqlite implementation. This is part of my commitment to use more Apple APIs where it makes sense, and this one made a lot of sense. While the migration took me a couple of days, the benefits [...]]]></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%2Fwww.freetimestudios.com%2F2009%2F11%2F11%2Fcore-data-tips-for-iphone-devs-part-1-command-line-shortcuts%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.freetimestudios.com%2F2009%2F11%2F11%2Fcore-data-tips-for-iphone-devs-part-1-command-line-shortcuts%2F&amp;source=neror&amp;style=normal&amp;service=bit.ly&amp;service_api=R_1991eb0e663e684f67b85bf3b2ecd1f8&amp;b=2" height="61" width="50" />
			</a>
		</div><div class="document">


<p>I recently finished migrating the data management backend for all of my games to Core Data from a custom sqlite implementation. This is part of my commitment to use more Apple APIs where it makes sense, and this one made a lot of sense. While the migration took me a couple of days, the benefits of using Core Data have already paid back that investment in improved performance and better data design. Among other things, my domain objects are now much more granular which allows me to pull only the little bits of data that I need when I need them. Core Data makes managing the relationships between all of these little domain objects dead simple. In other words, I don&#8217;t have to write any more <tt class="docutils literal">join</tt> queries!</p>
<p>Core Data is a massive topic, and I have only begun to scratch the surface of its usefulness. Still, I have found myself doing some repetitive tasks both at the command line and in code. Like any self respecting programmer, I hate repetitive tasks, and my Makefile and header file of macros have grown as a result. Since most of the shortcuts I have come up with will probably be useful to others, I am putting them together in a series of blog posts. This is the first of those posts.</p>
<div class="section" id="the-problem">
<h2>The Problem</h2>
<p>Core Data allows you to choose from multiple backend storage methods, and, other than having to choose a type, the data store is completely transparent. The recommended store on the iPhone is sqlite, and not only is it fast, but the schema generated by Core Data is fairly readable. Being able to poke around in the sqlite file was an immeasurable help while I was learning Core Data. The biggest problem is finding the sqlite database file. Every time you build and run your app in the simulator, the application directory is renamed with a new GUID which makes it really hard to find:</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 414px"><a href="http://www.freetimestudios.com/wp-content/uploads/2009/10/SimulatorAppWhackAMole.png"><img src="http://www.freetimestudios.com/wp-content/uploads/2009/10/SimulatorAppWhackAMole.png" alt="Can you find your app?" title="Simulator App Whack-A-Mole" width="404" height="283" class="size-full wp-image-146" /></a><p class="wp-caption-text">Can you find your app?</p></div>
</div>
<div class="section" id="the-solution">
<h2>The Solution</h2>
<p>Thankfully, OS X has the full compliment of unix command line tools. Wrap the proper commands in a Makefile, and you have some instant command line Core Data database management tools. Unix to the rescue! Here are the four make targets that I use the most:</p>
<div class="highlight"><pre><span class="nv">SQLITE_DB_FILE</span><span class="o">=</span>AppData.sqlite
<span class="nv">SIMULATOR_HOME</span><span class="o">=</span><span class="k">$(</span>HOME<span class="k">)</span>/Library/Application<span class="se">\ </span>Support/iPhone<span class="se">\ </span>Simulator

dbshell:
      find <span class="k">$(</span>SIMULATOR_HOME<span class="k">)</span> -name <span class="s2">&quot;$(SQLITE_DB_FILE)&quot;</span> -exec sqlite3 <span class="o">{}</span> <span class="s1">&#39;;&#39;</span>

killdb:
      find <span class="k">$(</span>SIMULATOR_HOME<span class="k">)</span> -name <span class="s2">&quot;$(SQLITE_DB_FILE)&quot;</span> -exec rm <span class="o">{}</span> <span class="s1">&#39;;&#39;</span>

dbschema:
      find <span class="k">$(</span>SIMULATOR_HOME<span class="k">)</span> -name <span class="s2">&quot;$(SQLITE_DB_FILE)&quot;</span> -exec sqlite3 <span class="o">{}</span> .schema <span class="s1">&#39;;&#39;</span>

dbdump:
      find <span class="k">$(</span>SIMULATOR_HOME<span class="k">)</span> -name <span class="s2">&quot;$(SQLITE_DB_FILE)&quot;</span> -exec sqlite3 <span class="o">{}</span> .dump <span class="s1">&#39;;&#39;</span>
</pre></div>
<p>The target names should make their use self explanatory, but I&#8217;ll summarize them:</p>
<dl class="docutils">
<dt><strong>dbshell</strong></dt>
<dd>Open up the sqlite command line shell with the Core Data database selected.</dd>
<dt><strong>killdb</strong></dt>
<dd>Delete the database file. This is useful if you&#8217;re making big changes to the schema and you would rather delete the whole database and start over rather than migrating the schema.</dd>
<dt><strong>dbschema</strong></dt>
<dd>Dumps the <tt class="docutils literal">CREATE</tt> statements required to recreate the schema. This is useful to see how Core Data persists your object graph.</dd>
<dt><strong>dbdump</strong></dt>
<dd>Dumps the schema as <tt class="docutils literal">CREATE</tt> statements and all of the data as <tt class="docutils literal">INSERT</tt> statements.</dd>
</dl>
<p>There is nothing magical or difficult about these, but I have found them to be very useful while developing my data model.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.freetimestudios.com/2009/11/11/core-data-tips-for-iphone-devs-part-1-command-line-shortcuts/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using xcache (Feed is rejected)
Page Caching using xcache
Database Caching 2/14 queries in 0.011 seconds using xcache
Object Caching 936/955 objects using xcache

Served from: www.freetimestudios.com @ 2012-02-04 21:05:26 -->
