Class

DistributedNotificationCenter

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.

Overview

Principal Attributes

Commonly Used Methods

default()

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.

Overview

Each task has a default distributed notification center that you access with the default() 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 macOS 10.3 and later, distributed notifications are always delivered to the main thread. For multithreaded applications running in 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.

Nested Types

Symbols

Getting Distributed Notification Centers

class func `default`()

Returns the default distributed notification center, representing the local notification center for the computer.

class func forType(DistributedNotificationCenter.CenterType)

Returns the distributed notification center for a particular notification center type.

Managing Observers

func addObserver(Any, selector: Selector, name: NSNotification.Name?, object: String?)

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

func addObserver(Any, selector: Selector, name: NSNotification.Name?, object: String?, suspensionBehavior: DistributedNotificationCenter.SuspensionBehavior)

Adds an entry to the receiver’s dispatch table with a specific observer and suspended-notifications behavior, and optional notification name and sender.

func removeObserver(Any, name: NSNotification.Name?, object: String?)

Removes matching entries from the receiver’s dispatch table.

Posting Notifications

func post(name: NSNotification.Name, object: String?)

Creates a notification, and posts it to the receiver.

func post(name: NSNotification.Name, object: String?, userInfo: [AnyHashable : Any]? = nil)

Creates a notification with information, and posts it to the receiver.

func postNotificationName(NSNotification.Name, object: String?, userInfo: [AnyHashable : Any]? = nil, deliverImmediately: Bool)

Creates a notification with information and an immediate-delivery specifier, and posts it to the receiver.

Suspending and Resuming Notification Delivery

var suspended: Bool

Suspends or resumes notification delivery.

Constants

Notification Center Type

This constant specifies the notification center type.

Notification Posting Behavior

These constants specify the behavior of notifications posted using the postNotificationName(_:object:userInfo:options:) method.

DistributedNotificationCenter.SuspensionBehavior

These constants specify the types of notification delivery suspension behaviors.

Relationships

Inherits From