<?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>Matteo Caldari &#187; coredata</title>
	<atom:link href="http://matteocaldari.it/tag/coredata/feed" rel="self" type="application/rss+xml" />
	<link>http://matteocaldari.it</link>
	<description>freelance developer</description>
	<lastBuildDate>Sat, 17 Sep 2011 14:01:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Multiple contexts, controllers, delegates and CoreData bug</title>
		<link>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug</link>
		<comments>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug#comments</comments>
		<pubDate>Mon, 09 Nov 2009 11:13:13 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[coredata]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://matteocaldari.it/?p=203</guid>
		<description><![CDATA[In a iPhone app I&#8217;m currently developing, I have a tipical Core Data object model with two entities (called Collection and Item) in a one-to-many relationship, and two table views for the entities, the second appearing when selecting a Collection in the first. The two table views have their own controller (which implements NSFetchedResultsControllerDelegate), managed [...]]]></description>
			<content:encoded><![CDATA[<p>In a iPhone app I&#8217;m currently developing, I have a tipical Core Data object model with two entities (called <em>Collection</em> and <em>Item</em>) in a one-to-many relationship, and two table views for the entities, the second appearing when selecting a <em>Collection</em> in the first. The two table views have their own controller (which implements <em>NSFetchedResultsControllerDelegate</em>), managed object context and <em>NSFetchedResultsController</em>. The second table has has a reference to the <em>Collection</em> object, used to build a predicate in this way:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/"><span style="color: #400080;">NSPredicate</span></a> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;collection == %@&quot;</span>, <span style="color: #002200;">&#91;</span>self.collection objectID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span></div></div>
<p><span id="more-203"></span><br />
In the first table view (the <em>collections</em>) everything works.<br />
In the second (the <em>items</em> of a <em>collection</em>) when a new object is added, none of the <strong>NSFetchedResultsControllerDelegate</strong> methods gets called.<br />
Moreover, when the name of an item is modified, the method <strong>controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:</strong> is called, but with type <strong>NSFetchedResultsChangeDelete</strong> instead of <strong>NSFetchedResultsChangeUpdate</strong>.</p>
<p>The problem is that, because of a bug in NSFetchedResultsController (see <a href="https://devforums.apple.com/message/139580#139580">this post</a> on the Apple Developer Forum) , the NSPredicate above does a pointer comparison between two objects that are in two different contexts, so a possibile workaround until it get fixed would be something like this:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/"><span style="color: #400080;">NSPredicate</span></a> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;collection == %@&quot;</span>, <span style="color: #002200;">&#91;</span>self.managedObjectContext objectWithID<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.collection objectID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</div></div>
]]></content:encoded>
			<wfw:commentRss>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

