简体   繁体   English

NSNotification如何运作

[英]How NSNotification works

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. ID。

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. 不要考虑尝试依赖任何与添加观察者的方式和时间相关的隐含顺序。

Basically the NotificationCenter keeps a reference to any object that is registered as an observer. 基本上,NotificationCenter会保留对注册为观察者的任何对象的引用。 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. 但您可以创建自己的,也许您希望确保您的通知对您的应用是私有的。

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. 现在,每个侦听名为@“notif_key”的通知的生物都可以执行某些操作。

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. 当第一个对象发送通知时,观察者对象将运行'doSomething:'方法。

Notes: 笔记:

  • userDict is a dictionary where you can send some info to those observers. userDict是一个字典,您可以在其中向这些观察者发送一些信息。
  • Don't forget to cancel the observer in the dealloc method. 不要忘记在dealloc方法中取消观察者。

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. 因此,通知可以在任何线程中注册,但与通知关联的方法在发布通知的线程上运行,因此如果我们要对UI进行任何更改,我们会将其分派给主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM