Search code examples
objective-ccocoanotificationskey-value-observing

3 notifications instead of one


I'm developing simple MVC app in Cocoa/Objective-C. I have a strange issue (or misunderstanding) with notifications and KVO.

I have AppController object in MainMenu.xib, hence I implement awakeFromNib method where I register for NSImageView changing its image property. I add self as an observer in the following way:

// options:3 equals to new/old passed values in changeDictionary
[backgroundImageView addObserver:self 
                      forKeyPath:@"image" 
                         options:3
                         context:NULL];

The backgroundImageView is an IBOutlet in AppController connected to NSImageView.

In standard observeValueForKeyPath:ofObject:change:context method I just log the received notification.

Problem is - when i change the image value of NSImageView I get 3 notifications instead of one. Can you help me with this? Maybe I'm overlooking something in options or in generally registering observer?

UPDATE: backgroundImageView is the instance of BackgroundImageView class which is sublcass of NSImageView. I subclassed the latter one for handling drag and drop operations as drag destination. When performDragOperation: is called (the last 'state' of the dragging) it changes the value for image property with setImage between willChangeValueForKey and didChangeValueForKey.


Solution

  • … it changes the value for image property with setImage between willChangeValueForKey and didChangeValueForKey.

    When you send an accessor message, you get KVO notifications for free with it. You should remove the {will,did}ChangeValueForKey: messages, because they're the cause of at least one of the extraneous change notifications.