<?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>wannabegeek</title>
	<atom:link href="http://www.wannabegeek.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.wannabegeek.com</link>
	<description>Things I find interesting - generally programming</description>
	<lastBuildDate>Mon, 25 Jan 2010 15:59:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Work around for &#8220;Auto Rearrange Content&#8221; for sorted Core Data NSArrayControllers</title>
		<link>http://www.wannabegeek.com/?p=28</link>
		<comments>http://www.wannabegeek.com/?p=28#comments</comments>
		<pubDate>Mon, 25 Jan 2010 15:59:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cocoa]]></category>

		<guid isPermaLink="false">http://www.wannabegeek.com/?p=28</guid>
		<description><![CDATA[The Problem
There seems to be a bug in Apples implementation of NSArrayController when &#8220;Auto Rearrange Content&#8221; is ticked in Interface Builder. You&#8217;ll typically be using this when sorting via a NSSortDescriptors.
I haven&#8217;t quite determined when this bug raises it&#8217;s head, but it looks to be when your NSTableView is bound to a ValueKeyPath via a [...]]]></description>
			<content:encoded><![CDATA[<h3>The Problem</h3>
<p>There seems to be a bug in Apples implementation of NSArrayController when &#8220;Auto Rearrange Content&#8221; is ticked in Interface Builder. You&#8217;ll typically be using this when sorting via a NSSortDescriptors.<br />
I haven&#8217;t quite determined when this bug raises it&#8217;s head, but it looks to be when your NSTableView is bound to a ValueKeyPath via a relationship. For example: A core data model with two entities &#8220;Value&#8221; and &#8220;Type&#8221;, Type and attribute &#8220;typeName&#8221; and Value has an attribute &#8220;name&#8221; and a relationship &#8220;type&#8221; connected to the &#8220;Type&#8221; entity.</p>
<p>We will need an NSArrayController set to use the entity &#8220;Value&#8221;. Now, if we create a NSTableView with two columns, one bound to the KeyPath &#8220;name&#8221; and the other to &#8220;type.typeName&#8221;. When the application is run, we will start seeing error logged to the console (you may need to start clicking on the NSTableView headers to resort)<br />
<code><br />
Cannot remove an observer for key path....<br />
</code></p>
<p>Also this doesn&#8217;t appear to be a problem when building a 64-bit application.</p>
<h3>The Solution</h3>
<p>I found the work-around for this was to sub-class NSArrayController, and subscribe to Core Data update notifications, on receipt of this check there is an update to one of our entities, and manually call <a href="http://devworld.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSArrayController_Class/Reference/Reference.html#//apple_ref/occ/instm/NSArrayController/rearrangeObjects">rearrangeContent</a>.</p>
<p>There is no real content to the interface file<br />
ArrayControllerFix.h</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Cocoa/Cocoa.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ArrayControllerFix <span style="color: #002200;">:</span> <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>&#8230;and only a small amount in the implementation file.<br />
ArrayControllerFix .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>awakeFromNib <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>super awakeFromNib<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>coreDataEntityDidChange<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>NSManagedObjectContextObjectsDidChangeNotification object<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span><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>coreDataEntityDidChange<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: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self sortDescriptors<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self sortDescriptors<span style="color: #002200;">&#93;</span> count <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>insertedEntities <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>note userInfo<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span>NSInsertedObjectsKey<span style="color: #002200;">&#93;</span> valueForKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;entity.name&quot;</span><span style="color: #002200;">&#93;</span>;
                <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>updatedEntities  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>note userInfo<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span>NSUpdatedObjectsKey<span style="color: #002200;">&#93;</span> valueForKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;entity.name&quot;</span><span style="color: #002200;">&#93;</span>;
                <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>deletedEntities  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>note userInfo<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span>NSDeletedObjectsKey<span style="color: #002200;">&#93;</span> valueForKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;entity.name&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
                <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>entityName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self entityName<span style="color: #002200;">&#93;</span>;
                <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>insertedEntities containsObject<span style="color: #002200;">:</span>entityName<span style="color: #002200;">&#93;</span> || <span style="color: #002200;">&#91;</span>updatedEntities containsObject<span style="color: #002200;">:</span>entityName<span style="color: #002200;">&#93;</span> || <span style="color: #002200;">&#91;</span>deletedEntities containsObject<span style="color: #002200;">:</span>entityName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                        <span style="color: #002200;">&#91;</span>super rearrangeObjects<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.wannabegeek.com/?feed=rss2&amp;p=28</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
