刚开始看Coalescing部分的时候有点疑惑,下面这个链接可以解惑 ^^`
http://stackoverflow.com/questions/4668372/coalescing-while-using-nsnotificationqueue
Notifications
The Foundation Framework includes a collection of APIs—the notifications supportclasses—that provide a powerful mechanism for event-driven programming. Anotification encapsulates information about an event. It can be sent to one or more observing objects in response to an event that occurs within a program. The notifications architecture follows a broadcast model; hence objects receiving events are decoupled from those sending them. The notification support classes enable the creation and posting of notifications, sending and receiving notifications, and asynchronous posting of notifications via queues. The APIs also support registering for notifications and the delivery of notifications to specific threads.
NSNotification encapsulates the information sent by a notification object. It consists of a unique name, the posting object, and (optionally) a dictionary of supplementary information. The class includes methods for creating and initializing notifications, and retrieving notification information. Listing 12-1 creates a notification named ApplicationDidHandleGreetingNotification for a simple greeting.
Listing 12-1. Retrieving the Network Host Name Using NSHost
NSString *nName = @"ApplicationDidHandleGreetingNotification";
NSNotification *notif = [NSNotification notificationWithName:nName
object:@"Hello, World!"];
So you may be wondering, why the long name for the notification? Well, Apple has provided some guidelines for the names of notifications. It recommends that a notification name be composed of the following elements:
- Name of the associated class
- The word Did or Will
- Unique part of the name
- The word Notification
These guidelines are designed to facilitate the creation of unique notification names that are self-descriptive.
The NSNotificationCenter and NSDistributedNotificationCenter classes provide the mechanisms for broadcasting notifications to interested observers. They include methods for getting the notification center instance, adding and removing notification observers, and posting notifications.NSDistributedNotificationCenter is a subclass of NSNotificationCenter that provides a mechanism for broadcasting notifications to other processes. The class method defaultCenter is used to retrieve the default notification center instanceassociated with an application, as shown in the following statement.
[NSNotificationCenter defaultCenter];
NSNotificationCenter provides several methods for posting notifications. The following statement uses the postNotification: method to post the notification created in Listing 12-1.
[[NSNotificationCenter defaultCenter] postNotification:notif];
NSNotificationCenter also provides methods to create and post a notification with a single method. The following statement uses thepostNotificationName:object: method to create and post a notification using the notification name created in Listing 12-1.
[[NSNotificationCenter defaultCenter] postNotificationName:nName
object:@"Salut!"];
NSNotificationCenter includes several methods for managing notification observers. You can register an object to receive a notification using theaddObserver:selector:name:object: method. This method takes parameters that specify the observer object to receive a notification, the message to send to the observer, the name of the notification for which to register the observer, and the sender from which the observer wants to receive notifications. Listing 12-2registers an object named handler (an instance of the Greeter class) to receive notifications from the variable named nName (see Listing 12-1), sending the message handleGreeting: to the observer.
Listing 12-2. Registering a Notification Observer
Greeter *handler = [Greeter new];
[[NSNotificationCenter defaultCenter] addObserver:handler
selector:@selector(handleGreeting:)
name:nName
object:nil];
Classes that receive notification events must implement a notification handler method with the following signature.
- (void) methodName :(NSNotification *)notification;
methodName is the name of the method. In Listing 12-2, the Greeter class implements a notification handler method named handleGreeting:.
NSNotificationCenter also provides methods to remove an observer; theremoveObserver: method unregisters an observer from notifications that it had previously registered for. The method removeObserver:name:object: is more fine-grained. It removes entries that match a specified observer, notification name, and sender. The following statement removes the notification with variable name nName for the observer named handler.
[[NSNotificationCenter defaultCenter] removeObserver:handler
name:uName
object:nil];
The NSNotificationQueue class provides a queue for notification centers, thereby enabling asynchronous posting and/or the coalescing of notifications. Coalescing is a process that enables you to filter (from a queue) notifications that are similar in some way to a notification that was queued earlier. The class includes methods to retrieve a notification queue instance, and add/remove notifications on the queue. The class method defaultQueue is used to retrieve the default notification queue instance for the current thread, as shown in the following statement.
[NSNotificationQueue defaultQueue];
The method initWithNotificationCenter: provides the capability to create a notification queue for a specified notification center. Listing 12-3 retrieves the default notification center and then creates a notification queue associated with it.
Listing 12-3. Creating a Notification Queue for a Specified Notification Center
NSNotificationCenter *notifier = [NSNotificationCenter defaultCenter];
NSNotificationQueue *queue = [[NSNotificationQueue alloc]
initWithNotificationCenter:notifier];
NSNotificationQueue includes two methods for posting notifications. TheenqueueNotification:postingStyle:coalesceMask:forModes: method enables you to specify the posting style, coalescing option, and supported run modes for posting the notification. The coalescing options are constant values defined as follows:
- NSNotificationNoCoalescing: Do not coalesce notification, post all.
- NSNotificationCoalescingOnName: Coalesce notifications with the same name, such that only one is posted.
- NSNotificationCoalescingOnSender: Coalesce notifications with the same sender, such that only one is posted.
The posting style defines the interaction mode (synchronous/asynchronous, when idle/as soon as possible) used to queue the notification. These options are specified as constant values, as follows:
- NSPostASAP: Asynchronously posts as soon as possible, when the current iteration of the corresponding run loop completes.
- NSPostWhenIdle: Asynchronously posts when the run loop is waiting for input source or timer events.
- NSPostNow: The notification queued is posted immediately (after coalescing), effectively providing synchronous behavior. This behavior does not require a run loop.
Listing 12-4 synchronously queues the notification assigned to the variable namednotif (see Listing 12-1), coalescing notifications with the same name.
Listing 12-4. Posting a Notification to a Queue with Coalescing
[[NSNotificationQueue defaultQueue]
enqueueNotification:notif
postingStyle:NSPostNow
coalesceMask:NSNotificationCoalescingOnName
forModes:nil];
NSNotificationQueue also includes a method to remove notifications from the queue. The following statement uses thedequeueNotificationsMatching:coalesceMask: method to dequeue notifications assigned to the variable named notif with no coalescing.
[[NSNotificationQueue defaultQueue]
dequeueNotificationsMatching:notif
coalesceMask:NSNotificationNoCoalescing];