在观察事件后删除观察者
我有一个对象观察者,我只需要检测一件事。一旦我完成了它,我想将其删除以消除开销。
所以它看起来像这样:
-(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
}
}
}
但这不起作用,并抛出一个错误,如:
NSKVOPendingNotificationRelease“EXC_BAD_ACCESS”
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:
NSKVOPendingNotificationRelease “EXC_BAD_ACCESS”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在
对象上调用removeobserver
吗?removeObserver:forKeyPath:
方法应该在接收器上调用。Shouldn't you be calling remove observer on the
object
?removeObserver:forKeyPath:
method should be called on the receiver.