Do not use self as your observation context.

Your observation context has to be unique for all observers of the object.  
“self” is a bad choice.

Also, you should only do your own work in the case that the observer is your 
observer. In the code below, you are also doing your work in the case where you 
should only be calling through to super.

—Jim

On Oct 6, 2011, at 5:47 AM, Stephan Michels wrote:

> I use KVO to execute custom code if a property has changed.
> 
> [self addObserver:self forKeyPath:@"graphics" 
> options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
> context:self];
> 
> - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
> change:(NSDictionary *)change context:(void *)context {
>    if (context != self) {
>        [super observeValueForKeyPath:keyPath ofObject:object change:change 
> context:context];
>    }
> 
>    if ([keyPath isEqualToString:@"graphics"]) {
>        [self setNeedsDisplay:YES];
>    }
> }
> 
> AFAIK the observe method will not be executed instantaneously, but on the 
> other side setNeedsDisplay has also a
> deferred execution.
> 
> Stephan.
_______________________________________________

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