views:

237

answers:

1

I am creating my own bindable custom treeview. For that I would like to observe NSTreeController for updates to its items' to-many-relationships. NSTreeController is bound to CD managed object context. Every depth level has its own CD Entity with parent/children/isLeaf properties. I need to maintain same hierarcy in the view (and order of items children). If something is inserted, i will start observing its children property. And i need to know indexpath for the newly inserted object ofcourse.

I would like to know exactly what was inserted/removed, so i can start observing it for insertions/removals to its children. As i understand, standard way to do this is looking into "change" NSDictionary inside observeValue:forKey..., but changes are NULL.

I know that this is a long time bug but is there some GOOD workaround for it? I've seen that some examples' views save arrays and then when the model changes, you compare for differences. Its more complicated for treeview. Also this way wastes memory and CPU cycles. I have one workaround that i am testing. It just kind of works so i will not describe it yet.

A: 

How about subclassing NSTreeController and implementing its insert/remove methods. Something like this for example.

- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath {

      // ... code to update your object relationships .. 
      // ... Take care here if you update any variables in your model (eg a sortindex) that would trigger KVO in the NSTreeController.  In those case you need to make the updates without triggering KVO by using setPrimitiveValue:forKey or get an infinite loop

      [super insertObject:object atArrangedObjectIndexPath:indexPath];

}
Ira Cooke