How NSNotification works

2019-05-02 15:36发布

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.

4条回答
倾城 Initia
2楼-- · 2019-05-02 16:01

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.

查看更多
forever°为你锁心
3楼-- · 2019-05-02 16:13

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.
查看更多
成全新的幸福
4楼-- · 2019-05-02 16:14

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.

查看更多
时光不老,我们不散
5楼-- · 2019-05-02 16:28

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.

查看更多
登录 后发表回答