Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"message was received but not handled" KVO

Tags:

I'm getting the following in my output window when running a project:

An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. Key path: connection.messageQueue Observed object: <Client: 0x10011ddb0> ... 

You get the idea. The thing is, I have no idea why this is happening. It appears everything is working fine, however. Here's the code causing the problem:

-(id) initWithIdentifier:(NSString*)ident andClient:(Client*)chatClient {     if(![super initWithNibName:@"ChatView" bundle:nil]) {         return nil;     }     [self setTitle: ident];     client = chatClient;     [self setIdentifier:ident];      inContext = false;      [client addObserver:self forKeyPath:@"connection.messageQueue" options:NSKeyValueObservingOptionNew context:NULL];     return self; }  -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     NSAttributedString* rar = [[NSAttributedString alloc] initWithString:@"test"];     [[textView textStorage] appendAttributedString:rar];     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } 

I thought I'd show all the code relating to this. The class only has a few methods so this is all you need to see. I'm not even using the change, I'm just plucking on "test" when the KVO event gets fired.

The stack trace gets pretty big very quickly as messages are coming in all the time. Yet it seems everything is working okay.

Any clues?

like image 602
Kezzer Avatar asked Sep 04 '12 19:09

Kezzer


2 Answers

You should not call [super observeValueForKeyPath:ofObject:change:context:] if you handle the notification. You should only call this for notifications you do not yourself handle.

like image 167
Rob Napier Avatar answered Sep 28 '22 03:09

Rob Napier


I have got the same problem: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. I have googled here, then I figured it out by myself, so maybe it helps someone.

For me, the problem was: the object registered as the observer (if you have ... addObserver:self ... then that object is self) has been released at the moment when the value was changed. But the observer was still registered, so -observeValueForKeyPath:ofObject:change:context: message was received by the nil observer.

like image 43
Dmitry Isaev Avatar answered Sep 28 '22 03:09

Dmitry Isaev