Hi,

there are two possible solutions for your problem:
nr.1: observe the NSManagedObjectContext via NSManagedObjectContextObjectsDidChangeNotification nr.2: observe arrangedObjects of your NSArrayController (as stated above, this is possible)

When you are recieving the notifications (either via nr1s NSNotificationCenter or in nr2s observeValueForKeyPath:ofObject:change:context:) in your custom control you have the chance to update your model. For nr2s approach you have to track the objects you are observing (via a private property of your custom view) and update it correspondingly. Additionally you probably want to observe keypaths of your model (to get notifications if individual propertys of your item changed.

Pseudocode:

@interface View
{
   NSArray *observedItems;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
   if ( context == kMyCollectionChangedContext ) {
      NSArray *changedItems = [ object valueForKey:keyPath ];
NSArray *newItems = all items from changedItems but not in observedItems; NSArray *removedItems = all items not in changedItems but in observedItems;
      [ self tearDownObservationOfItems:removedItems ];
      [ self addObservationOfItems:newItems ];
      self.observedItems = newItems;
      [ self updateUI ];
   }
   else if ( context == kMyIndividualPropertyChangedContext ) {
track which individual property changed and update UI (in your case modify the corresponding layer)
   }
}

- (void)addObservationOfItems:(NSArray*)newItems
{
   for ( id item in newItems ) {
[ item addObserver:self keyPath:theKeyPathForIndividiualProperty options:0 context:kMyIndividualPropertyChangedContext ];
   }
}

- (void)tearDownObservationOfItems:(NSArray*)oldItems
{
   for ( id item in oldItems ) {
[ item removeObserver:self forKeyPath:theKeyPathForIndividiualProperty ];
   }
}

You can easily add additional individual properties in addObservationOfItems:, just add your view as an observer for the corresponding keypath.

Regards, Markus



Am 23.08.10 09:32, schrieb Ajay Sabhaney:
I'm guessing, but you seem to have a managed object with a to-many 
relationship, and you want to be notified whenever anything is added to or 
removed from that relationship property. So, observe that property of the 
managed object, and you're done.

If there's something more complicated going on in the data model, your solution 
may be more complex, but throwing NSArrayController at the problem is highly 
unlikely to help with the solution.
Quincey, thank you for the follow up.  I do not think I explained my situation 
clearly so I will try and explain this more clearly.

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.

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.

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....

Comments and suggestions in relation to this as well as my original post are 
appreciated_______________________________________________

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/sumpfmonsterjunior%40googlemail.com

This email sent to sumpfmonsterjun...@googlemail.com

_______________________________________________

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