开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜