Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSDistributedNotificationCenter.h |
Related sample code |
The NSDistributedNotificationCenter
class 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 distributed notification center.
Notification dispatch table. See “Class at a Glance” > “Principal Attributes” in NSNotificationCenter Class Reference for information about the dispatch table.
In addition to the notification name and sender, dispatch table entries for distributed notification centers specify when the notification center delivers notifications to its observers. See the postNotificationName:object:userInfo:deliverImmediately:
method, “Suspending and Resuming Notification Delivery,” and NSNotificationSuspensionBehavior
for details.
defaultCenter
Accesses the default distributed notification center.
addObserver:selector:name:object:suspensionBehavior:
Registers an object to receive a notification with a specified behavior when notification delivery is suspended.
postNotificationName:object:userInfo:deliverImmediately:
Creates and posts a notification.
removeObserver:name:object:
Specifies that an object no longer wants to receive certain notifications.
The NSDistributedNotificationCenter
class implements a notification center that can distribute notifications asynchronously to tasks other than the one in which the notification was posted. An instance of this class are known as a distributed notification center.
Each task has a default distributed notification center that you access with the defaultCenter
class method. There may be different types of distributed notification centers. Currently there is a single type—NSLocalNotificationCenterType
. This type of distributed notification center handles notifications that can be sent between tasks on a single computer. For communication between tasks on different computers, use Distributed Objects Programming Topics.
Posting a distributed notification is an expensive operation. The notification gets sent to a system-wide server that 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, when too many notifications are posted and the server’s queue fills up, notifications may 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 NSDefaultRunLoopMode
, 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.
Note: NSDistributedNotificationCenter
objects should not be used to send notifications between threads within the same task. Use Distributed Objects Programming Topics or the NSObject
method performSelectorOnMainThread:withObject:waitUntilDone:
, instead. You can also setup an NSPort
object to receive and distribute messages from other threads.
– addObserver:selector:name:object:
– addObserver:selector:name:object:suspensionBehavior:
– removeObserver:name:object:
– postNotificationName:object:
– postNotificationName:object:userInfo:
– postNotificationName:object:userInfo:deliverImmediately:
– postNotificationName:object:userInfo:options:
Returns the default distributed notification center, representing the local notification center for the computer.
+ (id)defaultCenter
Default distributed notification center for the computer.
This method calls notificationCenterForType:
with an argument of NSLocalNotificationCenterType
.
NSDistributedNotificationCenter.h
Returns the distributed notification center for a particular notification center type.
+ (NSDistributedNotificationCenter *)notificationCenterForType:(NSString *)notificationCenterType
Notification center type being inquired about.
Distributed notification center for notificationCenterType.
Currently only one type, NSLocalNotificationCenterType
, is supported.
NSDistributedNotificationCenter.h
Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(NSString *)notificationSender
Object registering as an observer. Must not be nil
.
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. Must not be 0
.
The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer. When nil
, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil
, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
This method calls addObserver:selector:name:object:suspensionBehavior:
with suspensionBehavior:
NSNotificationSuspensionBehaviorCoalesce
(described in “Constants”).
NSDistributedNotificationCenter.h
Adds an entry to the receiver’s dispatch table with a specific observer and suspended-notifications behavior, and optional notification name and sender.
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(NSString *)notificationSender suspensionBehavior:(NSNotificationSuspensionBehavior)suspendedDeliveryBehavior
Object registering as an observer. Must not be nil
.
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. Must not be 0
.
The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer. When nil
, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil
, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
Notification posting behavior when notification delivery is suspended.
The receiver does not retain notificationObserver. Therefore, you should always send removeObserver:
or removeObserver:name:object:
to the receiver before releasing notificationObserver.
NSDistributedNotificationCenter.h
Creates a notification, and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender
Name of the notification to post. Must not be nil
.
Sender of the notification. May be nil
.
This method invokes postNotificationName:object:userInfo:deliverImmediately:
with userInfo:nil deliverImmediately:NO
.
– postNotificationName:object:userInfo:
– postNotificationName:object:userInfo:deliverImmediately:
– postNotificationName:object:userInfo:options:
NSDistributedNotificationCenter.h
Creates a notification with information, and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender userInfo:(NSDictionary *)notificationInfo
Name of the notification to post. Must not be nil
.
Sender of the notification. May be nil
.
Dictionary containing additional information. May be nil
.
This method invokes postNotificationName:object:userInfo:deliverImmediately:
with deliverImmediately:NO
.
– postNotificationName:object:
– postNotificationName:object:userInfo:deliverImmediately:
– postNotificationName:object:userInfo:options:
NSDistributedNotificationCenter.h
Creates a notification with information and an immediate-delivery specifier, and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender userInfo:(NSDictionary *)userInfo deliverImmediately:(BOOL)deliverImmediately
Name of the notification to post. Must not be nil
.
Sender of the notification. May be nil
.
Dictionary containing additional information. May be nil
.
Specifies when to deliver the notification. When NO
, the receiver delivers notifications to their observers according to the suspended-notification behavior specified in the corresponding dispatch table entry. When YES
, the receiver delivers the notification immediately to its observers.
This is the preferred method for posting notifications.
The notificationInfo 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 notificationInfo dictionary. See XML Property Lists for details.
– postNotificationName:object:
– postNotificationName:object:userInfo:
– postNotificationName:object:userInfo:options:
– encodeRootObject:
(NSArchiver)+ unarchiveObjectWithData:
(NSUnarchiver)NSDistributedNotificationCenter.h
Creates a notification with information, and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender userInfo:(NSDictionary *)userInfo options:(NSUInteger)notificationOptions
Name of the notification to post. Must not be nil
.
Sender of the notification. May be nil
.
Dictionary containing additional information. May be nil
.
Specifies how the notification is posted to the task and when to deliver it to its observers. See “Notification Posting Behavior” for details.
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 notificationInfo dictionary. See XML Property Lists for details.
– postNotificationName:object:
– postNotificationName:object:userInfo:
– postNotificationName:object:userInfo:deliverImmediately:
NSDistributedNotificationCenter.h
Removes matching entries from the receiver’s dispatch table.
- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(NSString *)notificationSender
Observer to remove from the dispatch table. Specify an observer to remove only entries for this observer. When nil
, the receiver does not use notification observers as criteria for removal.
Name of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When nil
, the receiver does not use notification names as criteria for removal.
Sender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When nil
, the receiver does not use notification senders as criteria for removal.
Be sure to invoke this method with notificationName:nil notificationSender:nil
(or removeObserver:
) before deallocating the observer object.
NSDistributedNotificationCenter.h
Suspends or resumes notification delivery.
- (void)setSuspended:(BOOL)suspended
YES
suspends notification delivery, NO
resumes it.
See NSNotificationSuspensionBehavior
for details on how the receiver delivers notifications to their observers when normal notification delivery is suspended.
The NSApplication
class automatically suspends distributed notification delivery when the application is not active. Applications based on the Application Kit framework should let AppKit manage the suspension of notification delivery. Foundation-only programs may have occasional need to use this method.
– addObserver:selector:name:object:suspensionBehavior:
– postNotificationName:object:userInfo:deliverImmediately:
– suspended
NSDistributedNotificationCenter.h
Returns a Boolean value that indicates whether notification delivery is suspended.
- (BOOL)suspended
YES
when notification delivery is suspended, otherwise NO
.
NSDistributedNotificationCenter.h
This constant specifies the notification center type.
FOUNDATION_EXPORT NSString * const NSLocalNotificationCenterType;
NSLocalNotificationCenterType
Distributes notifications to all tasks on the sender’s computer.
Available in Mac OS X v10.0 and later.
Declared in NSDistributedNotificationCenter.h
.
NSDistributedNotificationCenter.h
These constants specify the behavior of notifications posted using the postNotificationName:object:userInfo:options:
method.
enum { NSNotificationDeliverImmediately = (1 << 0), NSNotificationPostToAllSessions = (1 << 1) };
NSNotificationDeliverImmediately
When set, the notification is delivered immediately to all observers, regardless of their suspension behavior or suspension state. When not set, allows the normal suspension behavior of notification observers to take place.
Available in Mac OS X v10.3 and later.
Declared in NSDistributedNotificationCenter.h
.
NSNotificationPostToAllSessions
When set, the notification is posted to all sessions. When not set, the notification is sent only to applications within the same login session as the posting task.
Available in Mac OS X v10.3 and later.
Declared in NSDistributedNotificationCenter.h
.
NSDistributedNotificationCenter.h
These constants specify the types of notification delivery suspension behaviors.
typedef enum { NSNotificationSuspensionBehaviorDrop = 1, NSNotificationSuspensionBehaviorCoalesce = 2, NSNotificationSuspensionBehaviorHold = 3, NSNotificationSuspensionBehaviorDeliverImmediately = 4 } NSNotificationSuspensionBehavior;
NSNotificationSuspensionBehaviorDrop
The server does not queue any notifications with this name and object until setSuspended:
with an argument of NO
is called.
Available in Mac OS X v10.0 and later.
Declared in NSDistributedNotificationCenter.h
.
NSNotificationSuspensionBehaviorCoalesce
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, NSNotificationSuspensionBehaviorCoalesce
is the default.
Available in Mac OS X v10.0 and later.
Declared in NSDistributedNotificationCenter.h
.
NSNotificationSuspensionBehaviorHold
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.
Available in Mac OS X v10.0 and later.
Declared in NSDistributedNotificationCenter.h
.
NSNotificationSuspensionBehaviorDeliverImmediately
The server delivers notifications matching this registration irrespective of whether setSuspended:
with an argument of YES
has been called. When a notification with this suspension behavior is matched, it has the effect of first flushing any queued notifications. The effect is as if setSuspended:
with an argument of NO
were first called if the application is suspended, followed by the notification in question being delivered, followed by a transition back to the previous suspended or unsuspended state.
Available in Mac OS X v10.0 and later.
Declared in NSDistributedNotificationCenter.h
.
NSDistributedNotificationCenter.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-04-01)