I have a question for you guys. I was wondering I generate another thread and set NSNotificationCenter to observe a event in the main thread?, what will be the best of doing this?
I'll really appreciate your help
I have a question for you guys. I was wondering I generate another thread and set NSNotificationCenter to observe a event in the main thread?, what will be the best of doing this?
I'll really appreciate your help
Notifications are delivered synchronously by the poster. There are no queues involved. When you call postNotification:
, it will run all the observer code right then, sequentially for all observers, on the current thread, before returning.
If the observer wants to handle the observation on a different queue, then you should use dispatch_async
or an NSOperation
in the observer method to move the processing to the appropriate queue and then return.
You can handle notifications on a specific thread by using - (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block
.
As the documentation states, regarding the queue parameter:
The operation queue to which block should be added. If you pass nil, the block is run synchronously on the posting thread.