How NSNotification works
Asked Answered
L

4

8

I understand what in Notification, poster and observer.

But I am quite unable to understand how our app or OS understands and sends the flag/notification to the observer-class?

What is the mechanism behind this?

Your answer and help will be appreciated a lot.

Thanks

ID.

Luker answered 30/6, 2013 at 7:50 Comment(0)
M
7

Imagine the Notification Center as a dictionary which has keys of the notification names and values of the list of observers (and their specified action methods). When a notification is posted, the list of observers for that notification name is obtained and iterated. Each observer has its action method called with the notification information.

Also, during the iteration there is a check to determine if the notification object is of interest to the observer (based on the parameters supplied when the observer was added).

The notification process is carried out on the thread from which the notification was posted.

Don't think about trying to rely on any implied order related to how and when the observers were added.

Matthei answered 30/6, 2013 at 8:2 Comment(1)
So, if I am posting a notification having 'n' observers, does it cause an internal loop to iterate 'n' times?Venegas
K
4

Basically the NotificationCenter keeps a reference to any object that is registered as an observer. With that reference, it also keeps track of what kind of notifications that object wants. When an object posts a notification, the center delivers it to each registered observer by sending the observer a message with that selector.

The default center is normally a global singleton. But you could create your own, perhaps if you want to ensure your notifications are private to your app.

Keck answered 30/6, 2013 at 9:41 Comment(2)
how can we create our own notifications ?Chromatograph
Either by subclassing or not using the class factory method.Keck
I
1

In order to send a notification, an object sends:

[[NSNotificationCenter defaultCenter] postNotificationName:@"notif_key" object:nil userInfo:userDict];

Now, every living object that listen to a notification named @"notif_key" can do some action.

How do you make an object to listen?

It needs to run:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"notif_key" object:nil];

and When the first object will send the notification, the observer object will run 'doSomething:' method.

Notes:

  • userDict is a dictionary where you can send some info to those observers.
  • Don't forget to cancel the observer in the dealloc method.
Ithnan answered 30/6, 2013 at 8:1 Comment(0)
D
0

Add it to your understanding, which took some time for me to digest in to my head. Though it does not tell how it internally works, it tells how it is implemented to work

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

Source: apple documentation

So the notification can be registered in any thread, but the method associated with the notification is run on the thread on which the notification is posted, so if we want to make any changes to the UI we dispatch it to the main thread.

Despinadespise answered 26/7, 2016 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.