On 2014 Jul 07, at 13:42, Luc Van Bogaert <[email protected]> wrote:

> I'm observing the "selection" property of a NSArrayController object so I can 
> redraw a custom view whenever the selection of the controllers' objects 
> changes. This seems to work fine, except that I also receive messages for the 
> "selection" keypath when sending addObject: or removeObject: to the 
> controller. 
> The same happens when observing other keypaths like "selectedObjects" or 
> "selectedObjects.count".
> As a result I'm redrawing my custom view time many times when it's not 
> required. 
> 
> Is there a way to avoid this? 

In this case, I’d wonder if the selection really *is* changing, and in that 
case you’re getting what you ask for.  Or maybe it’s changing to nil and then 
changing back to the original selection.  In this case 
-observeValueForKeyPath:ofObject:change:context: should be invoked twice.

Problems like this can usually be solved by passing 
NSKeyValuyeObservingOptionNew and/or NSKeyValueObservingOptionOld as options to 
-addObserver:forKeyPath:options:context:, and processing the information 
contained in the change dictionaries through a cleverly-crafted filter.  You 
only perform your observer action (updating the view in your case) only for 
changes that pass through your filter.  Multiple observations can be coalesced 
by further filtering – create NSNotification objects with an an appropriate 
posting style and coalesce mask (See 
-enqueueNotification:postingStyle:coalesceMask:forModes:).  If it feels like a 
kludge, ignore your feelings.  It’s much cheaper processing than redrawing 
views.
_______________________________________________

Cocoa-dev mailing list ([email protected])

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

This email sent to [email protected]

Reply via email to