我有一个 NSOperation 子类,它实现了 setFinished: 来生成 KVO 通知:
-(void)setFinished:(BOOL)isFinished
{
LogError(@"%@ will will changing isFinished to %d",self,isFinished);
[self willChangeValueForKey:@"isFinished"];
LogError(@"%@ did will changing isFinished to %d",self,isFinished);
_isFinished = isFinished;
LogError(@"%@ will did changing isFinished to %d",self,isFinished);
[self didChangeValueForKey:@"isFinished"];
LogError(@"%@ did did changing isFinished to %d",self,isFinished);
}
我也确信我只将观察者添加到操作中一次。
奇怪的事情来了,有时候observeValueForKeyPath:ofObject:change:context:
对于这个对象的isFinished
key path被调用了两次,参考日志,我发现只setFinished:
调用了一次,而addObserver:forKeyPath:
对于这个操作只调用了一次。
另外,我发现它的顺序是这样的:
will will changing isFinished to 1
did will changing isFinished to 1
will did changing isFinished to 1
calling observeValueForKeyPath for object
did did changing isFinished to 1
calling observeValueForKeyPath for object
所以有人有任何线索吗?