views:

48

answers:

2

I want to know how long does it take from posting a notification to getting the notification.

The reason is that I want to find out if the observer pattern is suitable for me. I don't want that another view controller can change the value before the notification has been sent and processed. I'm afraid that another process (thread?) is faster and the value will be overwritten when it shouldn't.

A: 

you can use enqueueNotification for finer grained control over the processing of the notifications, but in the end I believe you can run into the same issue you have expressed concern about regardless of a NotificationCenter implementation or not

Aaron Saunders
+3  A: 

A notification center delivers messages synchronously, which means that the postNotification: method does not return until all objects registered to receive the notification have processed the notification. In other words, you can think of it as taking no time between posting a notification and receiving the notification.

There are a few extra things you'll need to be aware of:

Notifications are received on the same thread in which they are posted. If you move a notification over to the main thread using performSelectorOnMainThread:withObject:waitUntilDone:, you can break the synchronous behavior if waitUntilDone is set to NO. If waitUntilDone is set to YES, the thread passing the notification will block until the main thread has finished performing the specified action.

There is no guarantee of the order in which a notification will be received by its observers. If a single notification has multiple observers, don't rely on those observers receiving the notification in any particular order.

Given the above, and knowing which thread is posting notifications in your application and which thread needs to process them, you should be able to figure out whether the observer pattern will work for you.

James Huddleston
According to your informations it should work for me to use the observer pattern. I'm having only one thread (the main thread) and there will be only one observer (if the passing thread is blocking other operations).
testing
That sounds right to me. If you only have one observer and the notification is posted and handled on the main thread, that's about as uncomplicated as it gets. If you haven't created any other threads that access the value, you shouldn't have any problems.
James Huddleston