Reputation: 21
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?
Upvotes: 0
Views: 427
Reputation: 12055
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.Upvotes: 3