Removing an Observer After an Observed Event
I have an object observer that I only need to detect one thing. Once I'm done with it, I'd like to remove it to eliminate overhead.
So it would look something like this:
-(void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*) change context:(void*)context{
if ([keyPath isEqual:@"doingSomething"]){
if ([object isDoingSomething] == NO) {
[my_object setDoingSomething: DO_NOTHING_FOREVER];
[my_object removeObserver:self forKeyPath:@"doingSomething"] // <= ERROR eventually
}
}
}
This does not work though and throws an error like:
NSKVOPendingNo开发者_开发知识库tificationRelease “EXC_BAD_ACCESS”
Shouldn't you be calling remove observer on the object
?
[object removeObserver:self forKeyPath:@"doingSomething"];
removeObserver:forKeyPath:
method should be called on the receiver.
精彩评论