(Abstract) nsnotification notification for cocoa

Source: Internet
Author: User
Tags notification center
ArticleDirectory
    • Notification queue

From: http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_7.html

 

The announcement center synchronously distributes the announcement to its observer. Objects that issue notices are not re-obtained until all notices are issued.Program. To send a notification asynchronously, you must use the notification queue.

 

Nsnotificationcenter

Each task has a default notification center.NsnotificationcenterOfDefacenter CenterClass. The announcement Center processes the announcement in a single task. If you need to communicate between different tasks of the same machine, you can use the distributed notification center.

The announcement center synchronously sends the announcement to the observer. In other words, when a notification is issued, control of the program is not returned to the sender until all observers receive and process the notification. You can use the notification queue to send notifications asynchronously."Notification queue".

In multi-threaded applications, notifications are always transmitted in the sending thread, which may be different from the thread where the observer registers.

Nsdistributednotificationcenter

Each task has a default distributed notification center.NsdistributednotificationcenterOfDefacenter CenterClass. This distributed notice center is responsible for handling notifications between different tasks of the same machine. If you need to implement task communication between different machines, use distributed objects.

Sending distributed notifications is an expensive operation. The announcement will be sent to a system-level server and then distributed to the task where the objects registered with the distributed announcement are located. There is a high latency between sending a notification and reaching another task. In fact, if too many notifications are sent, so that the queue is full of servers, they may be discarded.

Distributed notifications are distributed by running cycles of tasks. The task must run in a common running cycle, suchNsdefaultrunloopmodeTo receive distributed notifications. If the task that receives the notification is multi-threaded, do not make the notification reach the main thread. Notifications are usually distributed to the running cycle of the main thread, but other threads can also receive notifications.

Although the conventional announcement center allows any object as the notification object (that is, the object encapsulated by the announcement), the distributed announcement center only supportsNsstringObject as its notification object. Since the objects to be advertised and the observer to be advertised may be in different tasks, the announcement cannot contain pointers to any objects. Therefore, the distributed notification center requires that the announcement use a string as the notification object. The advertised match is based on this string, not the object pointer.

Notification queue

NsnotificationqueueAn object (or simply called a notification Queue) acts as a notification center (NsnotificationcenterInstance. The announcement queue usually maintains the announcement in the FIFO order. When a notification is raised to the front of the queue, the queue sends it to the notification center, which then distributes it to all objects registered as observers.

Each thread has a default notification queue, which is associated with the default notification center of the task.Figure 5-9This association is displayed. You can create your own notification queue so that each thread and notification center has multiple queues.

Notice of aggregation

NsnotificationqueueClass adds two important features to the announcement mechanism of foundation kit: Aggregation and asynchronous sending of notifications. Aggregation is the process of removing other announcements similar to those that have just entered the queue from the queue. If a new announcement is similar to an announcement already in the queue, the new announcement does not enter the queue, and all similar announcements (except the first announcement in the queue) are removed. However, you should not rely on this special aggregation behavior.

You canEnqueuenotification: postingstyle: coalescemask: formodes:The third parameter of the method specifies one or more constants to indicate the simplified conditions:

    • Nsnotificationnocoalescing

    • Nsnotificationcoalescingonname

    • Nsnotificationcoalescingonsender

You canNsnotificationcoalescingonnameAndNsnotificationcoalescingonsenderConstant to indicate that cocoa uses both the announcement name and notification object for aggregation. In that case, all the announcements with the same name and sender object as the ones that have just entered the queue will be merged.

Send notifications Asynchronously

PassNsnotificationcenterClassPostnotification:Method and its variants. You can send the notification to the notification center immediately. However, the call of this method is synchronous: Before the notification sending object can continue to execute the work of its thread, the announcement center must wait for the announcement to be distributed to all the observers and return control. However, you can alsoNsnotificationqueueOfEnqueuenotification: postingstyle:AndEnqueuenotification: postingstyle: coalescemask: formodes:The method puts the notification into the queue for asynchronous sending. After the notification is put into the queue, these methods immediately return the control to the calling object.

Cocoa clears the notification queue and sends notifications Based on the specified sending style and running cycle mode in the queuing method. The mode parameter specifies the running cycle mode in which the queue is cleared. For example, if you specifyNsmodalpanelrunloopmodeThe notification is sent only when the running cycle is in this mode. If the current running cycle is not in this mode, you need to wait until the next running cycle enters this mode.

You can send notifications to the notification queue in three ways:Nspostasap,Nspostwhenidle, AndNspostnowThese styles are described in the following sections.

Send as soon as possible

ToNspostasapNotifications that enter the queue will be sent to the notification center when the current iteration of the running cycle is completed, if the current running cycle mode matches the request mode (if the request mode is different from the current mode, the notification is sent when it enters the request mode ). Since the running cycle may carry out multiple calling branches (callout) in each iteration process, the announcement may be distributed when the current calling Branch exits and the control returns the running loop, or it may not be distributed. Other Call branches may occur first, such as timers, events triggered by other sources, or other asynchronous notifications are distributed.

You canNspostasapStyle is used to consume expensive resources, such as display servers. If there are many customers in the process of running a loop call BranchCodeIt is expensive to draw an image in the window buffer, and refresh the buffer content to display the server overhead after each painting. In this caseDraw...All notifications such as "flushtheserver" will be put into the queue, and the name and object will be specified for aggregation, and useNspostasapStyle. As a result, at the end of the running loop, only one of the announcements is distributed, and the window buffer is refreshed only once.

Send when idle

ToNspostwhenidleNotifications that enter the queue are issued only when the running cycle is in the waiting state. In this state, the input channel that runs the loop does not contain any events, including timer and asynchronous events. ToNspostwhenidleA typical example of entering the queue by style is when the user needs to display the byte length of text when typing text and other parts of the program. It is costly (and not particularly useful) to update the size of text input boxes after each character is entered by the user, especially when the user enters the text box quickly. In this case, cocoa queues the notifications such as "changethedisplayedsize" after each character is entered. At the same time, enable the aggregation switch and useNspostwhenidleStyle. When the user stops the input, only one "changethedisplayedsize" Announcement (due to aggregation) in the queue will be issued when the running loop enters the waiting status, and the display part will be refreshed. Please note that the running loop is about to exit (this happens when all input channels are out of date) and is not in the waiting status, so no notification will be issued.

Send now

notifications that enter the queue in the nspostnow style are immediately sent to the notification center after the aggregation. You can use the nspostnow style when no asynchronous call is required (or use the postnotification: method of nsicationicationcenter ). 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 is distributed, so that the observer object receives the notification and processes it. Of course, when you want to remove a similar announcement in the queue by means of aggregation, you should use enqueuenotification ... instead of using the postnotification: method.

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.