views:

595

answers:

1

Appears the 3.0 NDA has been lifted, so this should be safe to ask. If this is violating an NDA, please let me know so I can remove the post, post-haste.

I have a very trivial implementation for KVO on an NSOperationQueue. My problem is that when compiling against 2.2.1 SDK, I get different results for the NSOperationQueue in question for a device using 3.0 or one using 2.2.1. I've confirmed this on 1 iPod Touch that has 2.2.1, one that has 3.0, and two sets of iPhones with similar setups.

The code looks like this:

// set observer
[self.myOperationQueue addObserver:self forKeyPath:@"operations" options:NSKeyValueObservingOptionNew context:NULL];

// implementation
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
    if ([keyPath isEqualToString:@"operations"]) {
      NSInteger operationCount = [[(NSOperationQueue*)object operations] count];
      NSArray *operations = [(NSOperationQueue*)object operations];
    }
}

As per the code above, the device running 3.0 will return the correct operationCount and the correct operations. A device running 2.2.1 will always return nil for operations and 0 for operationCount.

Can't seem to pin point why this is the case. All builds are compiled against 2.2.1.

NOTE

As per Matt's response below; 'object' is nil on 2.2.1. It is not nil on 3.0.

+1  A: 

The values you're getting sound like the object parameter is literally nil. To find the source of the problem, you may wish to check that object and self.myOperationQueue are the same value -- if they are then your self.myOperationQueue is just in a weird state. If they're not equal, then you can just read from self.myOperationQueue instead of object.

However, you are using the NSKeyValueObservingOptionNew option but you're not reading the value that it gives you. The point of NSKeyValueObservingOptionNew is that it passes the new value in the change dictionary.

i.e.

// Inside the observe method
NSArray *newOperationsArray = [change objectForKey:NSKeyValueChangeNewKey];

If you don't want to extract the new value from the change dictionary, you don't need to pass the NSKeyValueObservingOptionNew value (you can just pass 0).

Matt Gallagher
Hey Matt, you're most certainly right when you guessed that object is nil. I'm baffled as to why object is nil when it's ran on a 2.2.1 device but not on a 3.0 device..
Coocoo4Cocoa