observeValueForKeyPath is not getting called

user2122350 picture user2122350 · Aug 12, 2013 · Viewed 7k times · Source

I am developing a test app in which I have an NSOperationQueue. I am creating an NSInvocationOperation and observing the "isFinished" property of that operation. Strangely the observeValueForKeyPath is called only sometimes. I am not able to understand the changes i have to make to get it called every time. Please help.

Here is the code i have written :

-(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];
    }
}

Answer

ipmcc picture ipmcc · Aug 13, 2013

The following code works for me. I started from the iOS single view app template. Here is what I had:

@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

In the console, I see:

2013-08-13 08:04:15.150 TestOpKVO[71373:20b] Op ran
2013-08-13 08:04:21.903 TestOpKVO[71373:20b] came in

So something about your implementation of -CreateOperationWithContext: is causing trouble. That said, I still see the KVO notification getting called even if I change the operation to throw an exception.

If I were you I would start from this very basic, working example and then proceed one step at a time to adapt it to your real code, checking at each step to make sure the notification is still working.

A few tips: (that are probably unrelated to the problem you're seeing, but good practices for using KVO)

First, use KVO contexts with your observations. It's safer and more deterministic. See the answer I wrote over here for details.

Second, don't call -removeObserver:forKeyPath: from inside a call of -observeValueForKeyPath: (or -addObserver:... either) for the same keyPath that is being notified for. This has the potential to mess up KVO's internal observer data structures and can cause non-deterministic crashes that can drive you nuts. See the answer I wrote over here for details.