Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Package | com.apple.cocoa.foundation |
Companion guide |
NSDistributedNotificationCenter provides a way to send notifications to objects in other tasks. It takes NSNotification objects and broadcasts them to any objects in other tasks that have registered for the notification with their task’s default NSDistributedNotificationCenter.
A table of objects that want to receive notifications, the notifications they want to receive, and identifying strings they are interested in
Each task has a default distributed notification center. You typically don’t create your own.
defaultCenter
Accesses the default notification center.
addObserver
Registers an object to receive a notification with a specified behavior when notification delivery is suspended.
postNotification
Creates and posts a notification.
An NSDistributedNotificationCenter object (or simply, distributed notification center) is a notification center that can distribute notifications asynchronously to tasks other than the one in which the notification was posted.
Each task has a default distributed notification center that you access with the defaultCenter
static method. There may be different types of distributed notification centers. Right now there is a single type—LocalNotificationCenterType
. This type of distributed notification center handles notifications that can be sent between tasks on a single machine. For communication between tasks on different machines, use “Distributed Objects”.
Posting a distributed notification is an expensive operation. The notification gets sent to a system-wide server that then distributes it to all the tasks that have objects registered for distributed notifications. The latency between posting the notification and the notification’s arrival in another task is unbounded. In fact, if too many notifications are being posted and the server’s queue fills up, notifications can be dropped.
Distributed notifications are delivered via a task’s run loop. A task must be running a run loop in one of the “common” modes, such as NSRunLoop.DefaultRunLoopMode
, to receive a distributed notification. For multithreaded applications running in Mac OS X v10.3 and later, distributed notifications are always delivered to the main thread. For multithreaded applications running in Mac OS X v10.2.8 and earlier, notifications are delivered to the thread that first used the distributed notifications API, which in most cases is the main thread.
Creates an empty NSDistributedNotificationCenter.
public NSDistributedNotificationCenter
()
This center is not the default notification center. To obtain the default center, use defaultCenter
.
Returns the default distributed notification center, representing the local notification center for the machine by calling notificationCenterForType
with an argument of LocalNotificationCenterType
.
public static NSNotificationCenter defaultCenter
()
Returns the distributed notification center for the specified type.
public static NSDistributedNotificationCenter notificationCenterForType
(String type)
Currently only one type, LocalNotificationCenterType
, is supported.
Registers anObserver to receive notifications with the name notificationName and/or the identifying string anObject.
public void addObserver
(Object anObserver, NSSelector aSelector, String notificationName, String anObject, int suspensionBehavior)
When a notification of name notificationName with the identifying string anObject is posted, anObserver receives an aSelector message with this notification as the argument. The method for the selector specified in aSelector must have one and only one argument. If notificationName is null
, the notification center notifies the observer of all notifications with an identifying string matching anObject. If anObject is null
, the notification center notifies the observer of all notifications with the name notificationName. The suspensionBehavior determines how the notification center handles notifications when notification delivery has been suspended. The possible values are described in “Constants.”
Creates a notification with the name notificationName, associates it with the string anObject and dictionary userInfo, and posts it to the notification center with delivery scheduled for deliverImmediately, as supplied by the invoker.
public void postNotification
(String notificationName, String anObject, NSDictionary userInfo, boolean deliverImmediately)
This method is the preferred method for posting notifications.
The userInfo dictionary is serialized as a property list, so it can be passed to another task. In the receiving task, it is deserialized back into a dictionary. This serialization imposes some restrictions on the objects that can be placed in the userInfo dictionary. See “XML Property Lists” for details.
Posting with deliverImmediately set to false
allows the normal suspension behavior of the observers to take place. If deliverImmediately is set to true
, the notification is delivered immediately to all observers, regardless of their suspension behavior or suspension state.
Creates a notification with the name notificationName, associates it with the string anObject and dictionary userInfo, and posts it to the notification center.
public void postNotification
(String name, String anObject, NSDictionary userInfo, int options)
Possible values for options are described in the “Constants” section. Pass in 0 for no options.
The userInfo dictionary is serialized as a property list, so it can be passed to another task. In the receiving task, it is deserialized back into a dictionary. This serialization imposes some restrictions on the objects that can be placed in the userInfo dictionary. See “XML Property Lists” for details.
encodeRootObject
(NSArchiver)unarchiveObjectWithData
(NSUnarchiver)Suspends notification delivery when set to true
and resumes immediate notification delivery when set to false
.
public void setSuspended
(boolean suspended)
Distributed notification centers enable or suspend notification delivery on a per-task basis. When a task suspends notification delivery, notifications are delivered according to the suspension behavior of the observer. When delivery is not suspended, notifications are always delivered immediately. See “Constants” for the available types of suspension behaviors.
NSApplication automatically suspends delivery when the application is not active. Applications based on the Application Kit should let the Application Kit manage the suspension of distributed notification delivery. Foundation-only programs may have occasional need to use this method.
Returns true
if the notification center is delivering notifications for this application according to their suspension behavior, false
if it is delivering them immediately.
public boolean suspended
()
Applications based on the Application Kit should let the Application Kit manage the suspension of distributed notification delivery. Foundation-only programs may have occasional need to use this method.
NSDistributedNotificationCenter defines the following notification center type:
Constant | Description |
---|---|
LocalNotificationCenterType | Distributes notifications to all tasks on the sender’s machine. |
There are four different types of suspension behavior, each useful in different circumstances:
Constant | Description |
---|---|
NotificationSuspensionBehaviorDrop | The server does not queue any notifications with this name and object until |
NotificationSuspensionBehaviorCoalesce | The server only queues the last notification of the specified name and object; earlier notifications are dropped. In cover methods for which suspension behavior is not an explicit argument, |
NotificationSuspensionBehaviorHold | The server holds all matching notifications until the queue has been filled (queue size determined by the server), at which point the server may flush queued notifications. |
NotificationSuspensionBehaviorDeliverImmediately | The server delivers notifications matching this registration irrespective of whether |
NSDistributedNotificationCenter defines these constants to specify the behavior of notifications posted using postNotification
:
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)