0

I have an NSArrayController (itemsController) which hold an array of MyObject. This controller is tied to an NSTableView which has several columns, all bound to different properties.

I want to observe one of these properties elsewhere in the app.

[[self itemsController] addObserver:self forKeyPath:@"selectedObjects.someProperty" options:NSKeyValueObservingOptionNew context:nil];

In the callback:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

I am getting called whenever any property in the selection changes, but I only want to be called when someProperty in a selected object changes.

How can I prevent all these other calls when nothing has really changed?

Observing @"selection.someProperty" has the same results.

Trygve
  • 1,317
  • 10
  • 27
  • I find this hard to believe. There's no way to observe *all* properties of an object. How are you changing the properties? What `keyPath` and `change` dictionary are you receiving? Are you explicitly calling `-will/didChangeValueForKey:` or similar methods? Are you overriding KVC or KVO methods (e.g. `-setValue:forKey:`)? Is the array controller doing filtering or sorting? Is `automaticallyRearrangesObjects` enabled? By the way, there's no question: you should be binding through `selection` and **not** `selectedObjects`. – Ken Thomases Feb 18 '17 at 17:30

1 Answers1

1

You also get notified when selectedObjects or selection changes. If you want to get notified when someProperty changes then you have to observe someProperty of the selected objects and remove and add observers when the selection changes.

The superclasses of your observing class could also be observing. Use the context parameter to recognize your notifications. See Best practices for context parameter in addObserver (KVO)

Community
  • 1
  • 1
Willeke
  • 14,578
  • 4
  • 19
  • 47