observeValueForKeyPath没有被调用

时间:2013-08-12 14:03:48

标签: ios key-value-observing nsinvocationoperation

我正在开发一个测试应用程序,其中我有一个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];
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码适用于我。我从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

其次,请勿在{{1​​}}(或-removeObserver:forKeyPath:)的调用中针对正在通知的同一个keyPath调用-observeValueForKeyPath:。这有可能弄乱KVO的内部观察者数据结构,并可能导致非确定性崩溃,这可能会让你疯狂。有关详细信息,请参阅answer I wrote over here