NSArrayController + KVO + Core Data Question
NSArrayController + KVO + Core Data Question
- Subject: NSArrayController + KVO + Core Data Question
- From: Ajay Sabhaney <email@hidden>
- Date: Sun, 22 Aug 2010 16:55:21 -0600
Hello all,
In my Core Data based application, I have an NSArrayController, and it was necessary for me to receive a KVO notification when either a new item was added to the array controller, or removed from the array controller. By looking in to some forums, I found out that I cannot do this by observing the arrangedObjects property in NSArrayController - some say this is a bug. So instead, I binded the contentArray to a mutable array that I used to add and removed objects to or from. In addition, I added array accessors for the mutable array so that the KVO would work as expected. This now works beautifully, and I receive KVO notifications for object insertion and removal, as well as have the ability to see which objects were added or removed when observeValueForKeyPath:ofObject:change:context: is invoked.
However, in doing this, I lost some of the functionality that came for free with Core Data: undo/redo, load/save, etc. I suppose this is because Core Data makes the changes to arrangedObjects (and the mutable array binded to contentArray is not touched), and I am no longer observing arrangedObjects. I manually added some undo/redo functionality in the array accessors, but now thinking about how to do loading documents. I would like to stay away from observing arrangedObjects, as then I would receive two KVO notifications for adding and removing objects in to the NSArrayController (one through the contentArray observation and one through the arrangedObjects observation).
I also thought about subclassing NSArrayController so that addObject: performs a KVO notifications like so:
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"arrangedObjects"];
[super addObject:object];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"arrangedObjects"];
This would alleviate the need to use the contentArray property. However, I am assuming this would generate two KVO events - the new insertion notification (NSKeyValueChangeInsertion), and the existing change notification (NSKeyValueChangeSetting). I would like to avoid this.
So my question is, is there an easy and efficient way to achieve what I am trying to do?
Thank you in advance.
Ajay
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden