我正在开发一个具有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];
}
}
最佳答案
以下代码对我有用。我从iOS单视图应用程序模板开始。这是我所拥有的:
@implementation SOAppDelegate
{
NSOperationQueue* queue;
NSOperation* operation;
}
- (void)CreateOperationWithContext: (id)foo
{
NSLog(@"Op ran");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
queue = [NSOperationQueue new];
// operation = [NSInvocationOperation new]; // Commented this out because it's redundant with the next line
operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(CreateOperationWithContext:) object:[NSObject new]];
[operation addObserver:self forKeyPath:@"isFinished" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
[queue addOperation:operation];
return YES;
}
- (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];
}
}
// ... rest of empty default app delegate methods here...
@end
在控制台中,我看到:
2013-08-13 08:04:15.150 TestOpKVO[71373:20b] Op ran
2013-08-13 08:04:21.903 TestOpKVO[71373:20b] came in
因此,有关
-CreateOperationWithContext:
的实现会引起麻烦。也就是说,即使更改操作以引发异常,我仍然会看到KVO通知被调用。如果您是我,那么我将从这个非常基本的示例开始,然后一次执行一个步骤以使其适应您的实际代码,并在每一步骤中进行检查以确保通知仍然有效。
一些提示:(这可能与您遇到的问题无关,但是是使用KVO的良好做法)
首先,将KVO上下文用于您的观察。它更安全,更具确定性。有关详细信息,请参见answer I wrote over here。
其次,不要从被通知的相同keyPath的
-removeObserver:forKeyPath:
(或-observeValueForKeyPath:
)调用中调用-addObserver:...
。这可能会弄乱KVO的内部观察者数据结构,并可能导致不确定的崩溃,从而使您发疯。有关详细信息,请参见answer I wrote over here。关于ios - watchValueForKeyPath没有被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18189035/