Subject: Re: KVO Code Breaks Undo

On Mar 28, 2009, at 7:33 AM, Richard Somers wrote:

I have core data document based application with a custom opengl layer-hosting view. Everything works except when objects are added or removed from the managed object model the view is not redrawn. So I add the following to code to redraw the view but then automatic undo is broken.

// Receive KVO change notifications if objects are added to or removed
// from the array so we can redraw the display. !!!: Breaks undo!
- (void)awakeFromNib
[observableController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:NULL];

// Respond to KVO change notifications. !!!: Breaks undo!
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject: (id)object change:(NSDictionary *)change context:(void *)context
[graphicsLayer setNeedsDisplay];

Any ideas as to why undo stopped working?

You have implemented observeValueForKeyPath:... incorrectly. The main issue is that you fail to invoke super's implementation, but you are not using a context to distinguish your observation request from any observation request made by your superclass(es).

Try this: on a blank line outside of any method, type "observe" and then ask Xcode to complete the line. From the possible completions, select 'observeValueForKeyPath - observeValueForKeyPath: definition'. That will insert a template for the canonical implementation of observeValueForKeyPath:... into your code. Follow that template and you should do better.

You might also consider the approach outlined here:



Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)


ease do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to maillists@xxxxxxxxx