Instance Method

addObserverForName:object:queue:usingBlock:

Adds an entry to the notification center's dispatch table that includes a notification queue and a block to add to the queue, and an optional notification name and sender.

Declaration

- (id<NSObject>)addObserverForName:(NSNotificationName)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;

Parameters

name

The name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue.

If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.

obj

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

queue

The operation queue to which block should be added.

If you pass nil, the block is run synchronously on the posting thread.

block

The block to be executed when the notification is received.

The block is copied by the notification center and (the copy) held until the observer registration is removed.

The block takes one argument:

notification

The notification.

Return Value

An opaque object to act as the observer.

Discussion

If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).

The following example shows how you can register to receive locale change notifications.

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
    queue:mainQueue usingBlock:^(NSNotification *note) {
 
        NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
    }];

To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];

Another common pattern is to create a one-time notification by removing the observer from within the observation block, as in the following example.

NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [center addObserverForName:@"OneTimeNotification"
                                       object:nil
                                        queue:[NSOperationQueue mainQueue]
                                   usingBlock:^(NSNotification *note) {
                                       NSLog(@"Received the notification!");
                                       [center removeObserver:token];
                                   }];

See Also

Adding and Removing Notification Observers

addObserver:selector:name:object:

Adds an entry to the notification center's dispatch table with an observer and a notification selector, and an optional notification name and sender.

removeObserver:name:object:

Removes matching entries from the notification center's dispatch table.

removeObserver:

Removes all entries specifying a given observer from the notification center's dispatch table.