开发者

Removing an Observer After an Observed Event

开发者 https://www.devze.com 2023-03-15 02:30 出处:网络
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.

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

精彩评论

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

关注公众号