gpt4 book ai didi

iphone - 将 KVO 与 NSOperationQueue 一起使用时更改字典值错误?

转载 作者:行者123 更新时间:2023-12-03 20:00:21 24 4
gpt4 key购买 nike

我正在研究“更多 iPhone 3 开发”并发章节中的示例,但无法让 NSOperationQueue 上的 KVO 按预期工作。我创建一个 NSOperationQueue 并使用以下方法观察其操作数组:

NSOperationQueue *newQueue = [[NSOperationQueue alloc] init];
self.queue = newQueue;
[newQueue release];
[queue addObserver:self
forKeyPath:@"operations"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];

当第一个 NSOperation 添加到队列中时,我希望它被添加到其底层 operations 数组中(iOS 文档称其符合 KVO 标准)并且因此,在更改字典中,找到从 NSKeyValueChangeKindKeyNSKeyValueChangeInsertion 的映射,以及从 NSKeyValueChangeNewKey 到添加的 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 的子类,它适本地覆盖了 main,而 Stalled 只是项目但请注意,在插入单个操作时,该方法会被调用两次,并且两次的类型值为 1,即 NSKeyValueChangeSetting,而不是 NSKeyValueChangeInsertion。此外,newValueoldValue 似乎是数组本身,而不是添加的项目。

有什么想法吗?谢谢!

最佳答案

文档说 -operations 符合 KVO 标准,但没有指定通知的详细信息。在实践中,似乎只是告诉您发生了更改,因此必须比较新旧值才能找出插入的内容。

不要忘记这些通知可以在任何线程上发送给您!

关于iphone - 将 KVO 与 NSOperationQueue 一起使用时更改字典值错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4222055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com