Scheduling table
Notification hubs holds a schedule table with the following contents: Notifies the Observer (must exist), the notification name, and the notification sender.
The dispatch table for the notification hubs specifies the corresponding set of notifications for the observer, and a notification set is a subset of the notifications sent by the notification hubs.
There are 4 types of dispatch table entries, as shown in the following table:
(English version of the instructions)
Notification Name |
Notification Sender |
Notification set specified |
Specified |
Specified |
Notifications with a particular name from a special sender. |
Specified |
Unspecified |
Notifications with a particular name by any sender. |
Unspecified |
Specified |
Notifications posted by a specific sender. |
Unspecified |
Unspecified |
All notifications. |
(Chinese version instructions)
Notification name |
Notify Sender |
Specified set of notifications |
has been specified |
has been specified |
Specifies the notification of a specific name sent by the sender |
has been specified |
Not specified |
Notification of a specific name sent by any sender |
Not specified |
has been specified |
Specify all notifications sent by the sender |
Not specified |
Not specified |
All the Notifications |
The similarities and differences between notice and entrustment
Notifications and delegates, the notification mechanism is a good tool for implementing communication between objects in your application. It enables objects in the application to understand the changes that have occurred elsewhere. In general, an object is registered as an observer of the notification because it wants to adjust after the corresponding event has occurred or is about to occur.
But there are differences between notifications and delegates, and these differences also result in the two mechanisms being used in different places. The main difference between a notification model and a delegate model is that the former is a broadcast mechanism, and the delegate is a one-to-one relationship, and each model has its own merits.
Advantages of the notification mechanism:
• The notified object does not need to know the viewer's identity
• Any class can declare a notification, and its instance can publish a notification
• Notifications are not limited to intra-application communications; With distributed advertisements, a process can notify another process of events that occur.
Advantages of the delegation mechanism:
• The delegate has the opportunity to influence the event by returning the value to the object being delegated, and the notification is more reactive and can only affect itself and its environment when responding to an event.
• The delegate can affect how the delegate object handles the event (optionally specifying the parameter list of the method); The signature of the notification method is fixed.
• More efficient delegation than notification
Notice and KVO
In cocoa, the notification mechanism is not the only option to observe changes in the state of an object, and in many cases is not even the best option. KVO can also observe the changes of other objects, in the KVO, the observed object and the observer directly communicate, do not need like a notification center, such as intermediate objects, and therefore more effective than the notification mechanism.
However, in some cases, it is more reasonable to choose a notification than KVO. For example, you want to observe events rather than change the nature of the objects.
Notice Use note
The use of notifications requires knowing its impact on performance. When the notification is issued, it is distributed to the observer object synchronously through the local notification hub. If there are many observers, or if each observer has a lot of work to do with the notification, the program will have a noticeable delay. Therefore, use caution when using notifications, and do not use excessive, inefficient notifications. Here are some guidelines for using notification usage:
• Choose the notifications the application should observe
• When registering for notifications, specify the name of the notification and the object to be sent
• Implement the method of handling the advertisement as efficiently as possible
• Avoid adding or removing many observers
Classes in the notification mechanism
Class name |
Describe |
Nsnotification |
Notice |
Nsnotificationcenter |
Notification Center |
Nsdistributednotificationcenter |
Distributed notification hubs (interprocess communication is available in Mac OS x) |
Nsnotificationqueue |
Notification queue (Notification of coalescence and asynchronous send) |
The similarities and differences of nsnotification and entrustment in Notification Center and the main points needing attention