GCD 阻止不更新 NSCollectionView
我有一个 Cocoa 应用程序,它侦听通知并将更新发布到由 NSCollectionView
监视的 NSMutableArray
。通知大量到达,因此我正在考虑使用不同的队列来处理它们并相应地更新数组。
现在我正在使用 addObserverForName:object:queue:usingBlock 来注册通知,当我指定 时,它工作正常(数组和 NSCollectionView 都更新了) [NSOperationQueue mainQueue]
用于队列。但是,当我创建自己的队列(使用 [[NSOperationQueue alloc] init]
)时,NSCollectionView
停止更新。使用调试器,我可以看到它正在监视的数组正在更新。
这是一个错误,还是我在这里错过了什么?
I have a Cocoa app that listens for notification and posts updates to an NSMutableArray
monitored by a NSCollectionView
. The notifications arrive in large volumes so I was thinking to use a different queue to process them and update the array accordingly.
Right now I am using addObserverForName:object:queue:usingBlock
to register for notifications, and it works fine (both the array and NSCollectionView
are updated) when I specified [NSOperationQueue mainQueue]
for the queue. However when I created my own queue (using [[NSOperationQueue alloc] init]
), then the NSCollectionView
stops updating. Using the debugger I can see the array it's monitoring is being updated though.
Is this a bug, or did I miss something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 AppKit 绑定时,发布的任何 KVO 通知都需要在主线程上发生才能正常工作。因此,如果您直接从后台线程中的通知处理程序修改数组,NSCollectionView 将接收该线程而不是主线程上触发的任何 KVO 通知。发生这种情况时的行为是未定义的,最好的情况下不会工作,而最坏的情况下可能会导致崩溃或其他奇怪的行为。
如果通知数量确实足够多,以至于每个通知的更新都会造成性能问题,我建议您选择以下两件事之一:
-[NSOperationQueue addOperationWithBlock:]
是一种简单的方法。When working with AppKit bindings, any KVO notifications that get posted need to happen on the main thread in order for things to work correctly. So, if you modify the array directly from your notification handler in a background thread, the NSCollectionView will receive any triggered KVO notifications on that thread and not the main thread. The behavior when this happens is undefined, and at best won't work, while at worst could cause crashes or other strange behavior.
If the notifications do come in large enough quantities that updating on every notification is a performance problem, I'd recommend one of two things:
-[NSOperationQueue addOperationWithBlock:]
on the mainQueue is an easy way to do that.