On Aug 23, 2010, at 00:32, Ajay Sabhaney wrote:

> I have a very simple data model.  There is only one entity: Item.  Each time 
> I add or remove an Item to the array controller source, I would like my UI 
> view to take action based on the newly added/removed Item.  More 
> specifically: I have a very custom view, and each Item entity in the data 
> model corresponds to a Core Animation layer in the view.  So if a particular 
> Item entity is removed from the array controller, I'd like to remove a 
> corresponding CALayer from the layer-hosting view.  And if a particular Item 
> entity is added to the array controller, I'd like to add a corresponding 
> CALayer to the layer-hosting view.
> 
> Currently, my view is observing the arrangedObjects property of 
> NSArrayController.  However, all KVO notifications that the view receives are 
> NSKeyValueChangeSetting type changes (as opposed to NSKeyValueChangeInsertion 
> type changes).  I would like them to be NSKeyValueChangeInsertion changes so 
> that I may easily add a CALayer corresponding to the newly added Item model.

OK, this makes more sense now. The problem is that your NSArrayController is 
fetching *all* of the Items from the store (or perhaps only some, if you have a 
filter predicate, but the principle is the same). That means that there's no 
underlying relationship, and I think that's why you're not getting 
insert/delete change notifications for arrangedObjects.

Try using a technique similar to the Department/Employees Core Data tutorial in 
the documentation. That is, you can have a singleton object of a new entity, 
(say, ItemSet), which has a to-many relationship "items" to all of the Item 
objects. Bind the NSArrayController's content to the "items" property of the 
ItemSet object, and I think you'll find that arrangedObjects will give you the 
insert/delete notifications you want.

If it still doesn't do that (you don't have control over how the notifications 
are sent), your custom view can observe the "items" property directly, 
bypassing the NSArrayController for the purpose of keeping track of which 
layers are "alive". It could still use the NSArrayController to find out the 
desired order of the layers, if that matters, or it could simply avoid the 
NSArrayController completely. Note that the "items" property is unordered 
within the data model, but you can handle the sorting in the data model or in 
the custom view, without using the NSArrayController for that purpose, if you 
wish.

> So to overcome this, I binded the contentArray property of the 
> NSArrayController to a mutable array of mine.  So each time I add an Item to 
> the mutable array, the NSArrayController notifies my view, which takes the 
> appropriate action.
> 
> [ I am doing something somewhat similar to what you see on the LAST post in 
> this thread to ensure I receive NSKeyValueChangeInsertion type changes:
> http://stackoverflow.com/questions/1313709 ]
> 
> In case you're wondering why I'm using an NSArrayController and not just an 
> array, it is because NSArrayController currently ties in nicely with file 
> loading and saving, as well as undo and redo.  I'm also using some of the 
> selection functionality that NSArrayController provides.


NSArrayController has no functionality related to loading, saving, undo or 
redo. It can fetch objects from the store for you, and it can sometimes create 
managed objects for you. But NSArrayController is in no way *part of* Core Data.

> Thinking about this a little more carefully, I'm not sure that using an 
> NSArrayController actually is the best solution in this situation, especially 
> as the data model and the view become more complex....


Yes, I'm not sure that NSArrayController does anything to help you here. A 
properly designed data model is going to help you more.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to