我正在研究“更多 iPhone 3 开发”的并发章节中的一个示例,并且无法让 KVONSOperationQueue
按预期工作。我创建一个NSOperationQueue
并使用以下方法观察它的operations
数组:
NSOperationQueue *newQueue = [[NSOperationQueue alloc] init];
self.queue = newQueue;
[newQueue release];
[queue addObserver:self
forKeyPath:@"operations"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];
当第一个NSOperation
被添加到队列中时,我希望它被添加到它的底层operations
数组(iOS 文档说它是 KVO 兼容的),因此,在更改字典中,找到一个映射 from NSKeyValueChangeKindKey
to NSKeyValueChangeInsertion
,以及一个映射 fromNSKeyValueChangeNewKey
添加到NSOperation
. 但我没有看到任何价值NSKeyValueChangeInsertion
。
我知道调试器是专业的,但是为了在这里复制一些有用的东西,我开始了我的观察者方法:
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSNumber *kind = [change objectForKey:NSKeyValueChangeKindKey];
NSObject *newValue = [change objectForKey:NSKeyValueChangeNewKey];
NSObject *oldValue = [change objectForKey:NSKeyValueChangeOldKey];
NSIndexSet *indexes = [change objectForKey:NSKeyValueChangeIndexesKey];
NSLog(@"kind=%d, newValue=%@, oldValue=%@, indexes=%@",
[kind integerValue], newValue, oldValue, indexes);
那打印:
2010-11-18 20:01:56.249 Stalled[2692:6f07] kind=1, newValue=(
"<SquareRootOperation: 0x5f51b40>"
), oldValue=(
), indexes=(null)
2010-11-18 20:01:56.250 Stalled[2692:6f07] kind=1, newValue=(
"<SquareRootOperation: 0x5f51b40>"
), oldValue=(
"<SquareRootOperation: 0x5f51b40>"
), indexes=(null)
(SquareRootOperation
只是我适当NSOperation
覆盖的子类,并且只是项目名称。)但请注意,该方法在插入单个操作时被调用两次,并且两次都使用 kind 值 1,即not 。此外,似乎是数组本身,而不是添加的项目。main
Stalled
NSKeyValueChangeSetting
NSKeyValueChangeInsertion
newValue
oldValue
有任何想法吗?谢谢!