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 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.
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
Related
I have a QuickFIX initiator which receives frequent market data updates.
Altough i process each update as quickly as possible, still i have a concern regarding the callbacks.
Lets say, QuickFIX called my callback function and while i'm processing it, it's again called my function before the previous call is going on. What will happen in this situation? Is it guaranteed that i will be called for the next call or the engine can skip it because of the previous call is still going on?
Thanks
If the message is not malformed, then yes, a callback should be triggered for every message received.
(If it is malformed, the engine will reject automatically and will not pass the message to your application code.)
Incoming messages are actually collected in a queue while you are processing them. For this reason, you should not perform time-intensive operations in the the callbacks. If you have some lengthy processing, you should dispatch it to another thread so as to not cause the queue to back up.
Just wondering, does anyone have experience with activating large amounts of NSNotification observers at a time?
What is the overhead of an observer? Is it reasonable to run, say, 50 or 100 or more observers at a time?
I have an application that displays a scrolling list of media from database and I want to implement NSNotificationCenter as a scalable method of listening for individual pieces of media and allocating them to the proper UIViews
Cheers,
Doug
Notification does not have overheads if they are processed in background. If you process NSNotification in one single thread then any one of the observers can mishandle it which would lead to a blocking thread. If this thread happens to be the main thread (in your case it is ) then app will freeze .
It depends upon the way notification is handled. Sending notification is not an overhead they are just 50 -100 method calls just like any other methods. (I have 5000 methods in my app). The issue is thw way it is handled. If each observer blocks the notification for a long time then nothing can be done. I will suggest using NSNotification queues instead NSNotification Queue
If time permits I would also suggest to refer reading article on Objects Communication by apple.
I'm going to take J2theC's advice on this one..
I'm currently shifting the design pattern to use delegate methods to prevent any freezing..
Thanks for the feedback!
Is it possible to dispatch (send) events manually by calling [[GANTracker sharedTracker] dispatch] even though GANTracker was instantiated with dispatchPeriod set to e.g. 10 seconds?
My understanding of Google Analytics iOS framework is that upon calling trackEvent:action:label:value:withError: method the event is not actually sent, but rather queued internally in GANTracker which will send it when dispatchPeriod expires. In other words, GANTracker collects events, page views etc. and will send them in burst every X seconds as per positive value of dispatchPeriod. Am I right?
Now, the documentation says that in order to manually dispatch (send) events to server, I need to instantiate GANTracker with dispatchPeriod set to -1. Is it really necessary? Can't I have best of both worlds? I thought calling [[GANTracker sharedTracker] dispatch] will send events regardless of dispatch period...
edit: I know, I could test it, but 24hrs delay period for data propagation is a bit long...
Answering my own question, maybe someone will find it useful:
yes, it is possible to use dispatch when tracker is started with dispatchPeriod > 0.
I've tested it and GA events dispatched that way are visible in GA.
Is there a way to know if an object is already registered as an observer for a particular notification?
In my implementation I have to add and remove the observers on the fly. For some reason, there is a random issue where the listener is receiving twice the same notification. I know I have to review my coding but It will be easier to fix for me if I could know this info.
Thanks.
No. There is no way to query this information. If you need it, you need to keep track of that yourself.
You might want to look into NSNotificationQueue. Here's the overview from Apple. It sounds like this could help you stop receiving duplicate notifications:
NSNotificationQueue objects (or simply
notification queues) act as buffers
for notification centers (instances of
NSNotificationCenter). Whereas a
notification center distributes
notifications when posted,
notifications placed into the queue
can be delayed until the end of the
current pass through the run loop or
until the run loop is idle. Duplicate
notifications can also be coalesced so
that only one notification is sent
although multiple notifications are
posted. A notification queue maintains
notifications (instances of
NSNotification) generally in a first
in first out (FIFO) order. When a
notification rises to the front of the
queue, the queue posts it to the
notification center, which in turn
dispatches the notification to all
objects registered as observers.
Every thread has a default
notification queue, which is
associated with the default
notification center for the task. You
can create your own notification
queues and have multiple queues per
center and thread.
I am interested in knowing whether I can expect the observing object's method to be pushed onto the stack before the posting object's method has been completed and removed.
The short answer is yes... "Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread".
However, Apple has docs on this very subject that you may find helpful, and from which the above quote was pulled:
Notification Programming Topics: Delivering Notifications to Particular Threads
Just bothered to look it up myself, shameful:
From the class reference: NSNotificationCenter posts all notifications synchronously
Also, if you prefer, you can use NSNotificationQueue to post notifications asynchronously