Summary of notification

Source: Internet
Author: User
Tags notification center

Summary of Notification
  • The standard way to communicate between objects is to send a message, and an object calls the method of another object directly. The premise is that you need to know the contents of both the object and the message to be sent. And the coupling is so high that it binds two objects that are inherently independent.
  • Nsnotification contains a name, an object, and an optional dictionary
  • Any object may post a notification
  • Nsnotificationcenter processing of notifications between single processes
  • Nsdistributednotificationcenter handles notifications between different processes on a single computer.
  • Each process creates a notificationcenter, which is obtained by Nsnotificationcenter Defaultcenter.
  • Noticiationcenter sends a request in a synchronous (non-asynchronous, waiting, blocking) way, that is, when the notification is sent, center waits for all observer to receive and processes the notification before returning to poster. If you need to send notifications asynchronously, use the Notificationqueu
  • In a multithreaded application, notifications are sent to all threads.
  • Each process has a DISTRUBUTED notification center that can beNSDistributedNotificationCenter +defaultCenter 访问,这个distributed notification center 处理 单个机器不同线程之间的通知。若需要不同的机器之间通信,需要使用distrubted object
  • Sending distrubuted notification is a very resource-intensive operation, distrubted notification needs to be sent to a system-level service and distributed to a process with an object registration notification. The delay between the distrubted notification and the receipt of the notification is uncertain, and if too many notifications are sent out the day is filled with the system's sever, the distrubuted noification may be discarded.
  • Distributed notifications is sent through the runloop of the process. To accept the distrubuted notifiation, this process must run in the usual common run loop mode, such as Nsdefaultrunloopmode, if the accepted process is multithreaded, then do not expect the notification to be sent to the main thread, Although typically distrubuted notification is sent to the main thread, other threads may also receive notifications.
  • The generic notification contains an object that can be an object pointer, but the Distrubuted object must be a string because the pointers are different in different processes.
  • Nsnotificationqueue, it's like a notification buffer pool. He has two important feature, merging notifications and sending notifications asynchronously.
  • Nsnotificationqueue usually processes notifications in FIFO (first-in, FIFO), and when a notification is in front of the queue, queue sends him to notification Center, then notification Center then sends him to observer.
  • Each thread has a queue associated with notification center, and you can create your own queue, which forms a center and multiple queues.
  • 用NSNotificationQueue' s and enqueueNotification:postingStyle:enqueueNotification:postingStyle:coalesceMask:forModes:的方法,你可以把通知放到queue里面异步的发送给线程。吧通知放到queue以后这些方法会立刻返回,不做等待。
  • notification有三种style放到queue, NSPostASAPNSPostWhenIdle, and NSPostNow
  • You can set the third parameter to make the coalesce, multiple under the same notification in the queue can be merged dimension one.
Send As soon as possible

The NSPostASAP notification that the style enters the queue is sent to the notification center when the current iteration of the run loop is complete, if the current run cycle mode matches the requested pattern (if the requested pattern differs from the current mode, the advertisement is emitted when the requested mode is entered). Because the run loop may make multiple call branches (callout) during each iteration, the notification may or may not be distributed when the current call branch exits and the control returns to the run loop. Other invocation branches may occur first, such as timers or events triggered by other sources, or other asynchronous announcements are distributed.

You can often NSPostASAP use styles for expensive resources, such as displaying servers. It is expensive to flush the contents of the buffer to the display server after each draw, if there is a lot of customer code in the window buffer that is being painted during a call to the branch that runs the loop. In this case, each draw... method queues up a notification such as "Flushtheserver" and specifies that it be clustered by name and object, as well as using NSPostASAP styles. As a result, only one of those announcements is distributed at the end of the run cycle, and the window buffers are only refreshed once.

Send when Idle

NSPostWhenIdleannouncements that enter a queue in style are emitted only when the running loop is in the waiting state. In this state, there are no events in the input channel that runs the loop, including timers and asynchronous events. NSPostWhenIdleA typical example of entering a queue in style is when the user types text, and the other part of the program needs to display the length of the text byte. The cost of updating the size of the text input box after the user has entered each character is significant (and not particularly useful), especially when the user enters it quickly. In this case, cocoa will queue up announcements such as "Changethedisplayedsize" after each character type, while opening the knot switch and using NSPostWhenIdle style. When the user stops typing, only one "changethedisplayedsize" advertisement in the queue (due to the cause of the coalescence) is emitted while the loop is running into the waiting state, and the display is therefore refreshed. Note that the run loop is about to exit (this happens when all the input channels are obsolete) and therefore does not issue a notification.

Send Now

NSPostNowannouncements that enter the queue in style are immediately sent to the notification center after the knot is gathered. You can use the NSPostNow style (or send it by Nsnotificationcenter method) when you don't need to invoke the behavior asynchronously postNotification: . In many programming environments, we not only allow synchronous behavior, but also want to use this behavior: that is, you want the notification center to return after the announcement, so that the Observer object receives the notification and is processed. Of course, when you want to remove a similar announcement from a queue by coalescence, you should use enqueueNotification ... methods, and use NSPostNow styles instead of using postNotification: methods.

//=========================================================================

Notifications

The notification wraps information about the event, such as the window is gaining focus or the network connection is disconnecting. An object that needs to subscribe to an event (for example, a file wants to know that the window being edited is going to be closed) needs to be registered with Notification Center and then notified when the event occurs. When the event occurs, a notification is sent to notification center, and then notification Center forwards the notification to all objects that have subscribed to the event. Notification will be cached in the notification queue when needed ....

The principle of notification

The most standard way to pass information between two objects is to pass the message – an object that invokes a method of another object. However, this method of passing messages requires the object that sends the message to know the recipient of the message and the type of message it can receive. This will bind two objects tightly-most notably this will allow two independent subsystems to be coupled together. To solve these problems, the broadcast model was raised. object is only responsible for sending notification, and Nsnotificationcenter will be responsible for forwarding this notification to all relevant object.

A nsnotification (in this article abbreviation notification) contains a name, an object, and an optional dictionary. Name is the identity of the notification. object contains any type of object notification the sender wants to send (typically the object itself that sends the notification). Dictionary is used to save other related things (if any).

Any object can send notification. Any object can be registered with notification Center to be notified when an event occurs. Notification Center is responsible for sending the accepted Notification to all registered recipients of the message. Send notification object, notification contains the object and receive this notification object can be the same object, or it can be three different object. Sending notification object does not need to know any information about the recipient. However, the recipient needs to know at least the name of the notification and the key (if any) of the dictionary it contains.

Notification and delegation

In terms of use, the notification system is much like the delegate, but they have the following differences:

The recipient of the *notification can be more than one object. But delegate object can only have one. This prevents the return value.
* An object can accept any number of notification it wants from notification center, and delegate can only accept a predefined delegate method.
* Send notification object completely unaware of whether the recipient exists.

Notification Centers

Notification Center is responsible for receiving and sending Notification. When it receives the notification, it notifies all recipients who meet certain criteria. Notification information is packaged in Nsnotification. The notification receiver registers with the notification center to obtain the notification of the other object. When an event occurs, an object sends the relevant notification to notification Center. Notification Center sends a message to each registered recipient. The object and the recipient who sent the notification may be the same.

Cocoa consists of two notification center:

The *nsnotificationcenter class manages notification within a single process.
*nsdistributednotificationcenter manages cross-process notification on a single machine.

Nsnotificationcenter

Each process has a default notification center, and you can get it by accessing the Nsnotificationcenter +defaultcenter method. This type of notification Center is responsible for managing notification within a single process. If you need to manage notification between different processes on the same machine, you need to use Nsdistributednotificationcenter.

Notification Center sends the Notification to the receiver in a synchronous way. That is, when a notification is sent, it will not return unless all the recipients have received and processed the notification. If you want to send asynchronous notification, you need to use the notification queue.

In a multithreaded application, notification is always in the same thread as the sender, but the recipient can be in another thread.

Nsdistributednotificationcenter

Each process has a default distributed Notification Center, and you can get it by accessing the Nsdistributednotificationcenter +defaultcenter method. This type of notification Center is responsible for managing the notification between multiple processes on a single machine. If you need to communicate between multiple machines, use distributed objects.

Sending a distributed notification is very expensive. Notification is first sent to a system-level server and then distributed to each registered process separately. The delay between the sending from the message to the receipt of the message is theoretically infinite. In fact, if too many notification are sent to the server, the notification queue on the server may be full, which could cause notification to be lost.

Distributed notification will be sent out in the main loop of a process. A process must ensure that there is a primary loop running inside it, such as Nsdefaultrunloopmode, before it can accept the distributed notification. If the receiving process is multithreaded, then notification is not necessarily accepted by the main thread. In general, notification will be distributed to the main thread's primary loop, but other threads can receive the same.

A generic type of Notification Center can register all notification for an object, but distributed notification Center only registers a string type of notification. Because the sender and the recipient may be in a different process, the object contained within the notification cannot be guaranteed to point to the same object. Therefore, distributed Notification Center can only accept notification that contain string types. Notification are matched based on strings.

Summary of notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.