我正在开发一个测试应用程序,其中有一个 NSOperationQueue。我正在创建一个 NSInvocationOperation 并观察该操作的“isFinished”属性。奇怪的是,observeValueForKeyPath 只是有时被调用。我无法理解每次调用它时必须进行的更改。请帮忙。
这是我写的代码:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
........//initialization
queue = [NSOperationQueue new];
operation=[NSInvocationOperation new];
operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(CreateOperationWithContext:) object:context];
[operation addObserver:self forKeyPath:@"isFinished" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
[queue addOperation:operation];
..... // launch the view controller
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"isFinished"]) {
NSLog(@"came in");
[operation removeObserver:self forKeyPath:@"isFinished"];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}