Explaining this method in documentation


#1

[quote]addObserverForName:object:queue:usingBlock:
Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.

  • (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))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 you want to add the block to the operation queue.
    If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.
    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.[/quote]

I don’t understand this line

[quote]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 you want to add the block to the operation queue.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.[/quote]


#2

name Use this parameter to specify the name of a notification you (as an observer) are interested in. The notification center will execute the specified block when it receives a notification with this name.

obj Use this parameter to specify the object whose notifications you (as an observer) are interested in.

By specifying both of these parameters you can reduce the number and kinds of notifications you receive.

This is explained in detail in the Overview section of the NSNotificationCenter Class Reference.


#3

ibex10 can you explain more about the object part I don’t really understand your explanation can you give some example?


#4

A good example of NSNotifications is on pages 174/175:

UIDevice *device = [UIDevice currentDevice];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
       selector:@selector(orientationChanged:)
           name:UIDeviceOrientationDidChangeNotification
         object:device];

Here we are sending the “currentDevice” in the object part of the notification. The when this notification is posted the message is sent to orientationChanged: where we can get the orientation information on the currentDevice as we sent it within the notification:

- (void)orientationChanged:(NSNotification *)note
{
    NSLog(@"Orientation changed: %ld", [[note object] orientation]);
}

Of course, the example you gave was using blocks, so what is achieved within the orientationChanged: method would be done in the block.

Nick


#5

Hi Fujila I’m sorry but I’m not using the same edition of book that you’re having now.
In my notification sub topic in the book, they left the object to nil. here’s the code

[code] BNRLogger *logger = [[BNRLogger alloc] init];

    [[NSNotificationCenter defaultCenter]
                               addObserver:logger
                                  selector:@selector(zoneChange:)
                                      name:NSSystemTimeZoneDidChangeNotification
                                    object:nil];[/code]

#6

If you pass nil for the object parameter, then the notification center will send you notifications sent by any object but only those notifications matching the value passed in the name parameter.

You should read this: The Overview section of the NSNotificationCenter Class Reference.