Core Data and Managed Object Context Question
Core Data and Managed Object Context Question
- Subject: Core Data and Managed Object Context Question
- From: Ajay Sabhaney <email@hidden>
- Date: Wed, 8 Sep 2010 04:39:10 -0600
Hello list,
I have a Core Data based application with a simple data model. There is an entity 'ItemContainer' which has a to-many relationship with the entity 'Item'. An item model entity corresponds to a Core Animation layer, so when a new Item managed object is added to the ItemContainer managed object, a CALayer should be added to the view. Currently, I'm observing (using KVO) the ItemContainer managed object. Each time an item is added to the item container set, I receive a notification (change type is NSKeyValueChangeInsertion), and add a correspondingly add a Core Animation layer to the layer-hosting view. A similar sort of thing happens when an item is removed from the item container set as well. This all works very nicely - including the free undo and redo functionality provided by Core Data and the managed object context.
The problem is I'm not sure the best way to go about implementing document loading.
I know NSArrayController offers the ability to bind to the managed object context (which I obtain from my NSPersistentDocument), which makes loading + fetching very simple. However, if I use NSArrayController, and observe the arrangedObjects key of the array controller (as opposed to the item container set), I cannot get the newly added object (instead of getting NSKeyValueChangeInsertion type changes, I would get NSKeyValueChangeSetting type changes), which seems to be a problem that others have experienced as well. It is important to get only the newly added/removed items since we would like to be able easily and efficiently add/remove only the necessary CALayers from the root layer.
As an aside, it seems that if I bind the contentSet property of the NSArrayController to the ItemContainer's items set, the loading does not work anymore, and arrangedObjects is empty after loading a document (which makes sense since the source set is empty....).
So I'd like some insights on either how I could completely skip using the NSArrayController and automatically do a fetch after a document is loaded so that the model items become populated and I get the proper KVO notifications, OR, how to make the NSArrayController work in this context. Or perhaps I am missing something altogether?
If possible, I would like to avoid:
1) Using an NSArrayController and on every KVO notification, look at the items that existed before the notification, looking at the items that exist after the notification, and determine the newly added items given the difference.
2) Using NSArrayController for the sake of loading, then once the loading/fetching is complete, continue to observe the item container set and disregard the NSArrayController (although this probably would work okay).
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