user3090658
user3090658

Reputation: 157

Explaining apples documentation

I'm trying to understand this two parameters of the method but i don't seems to understand it. addObserverForName:object:queue:usingBlock:

I don't understand the description of 2 of the parameters in the method. Hope someone can explain it to me.

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block

I don't understand this 2 lines:

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.

what does it mean when the notification centre doesn't use notification name to decide whether to add block to the operation queue when it is nilled. also,what notification name should i put in. I don't know. and

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.

What object should i insert into this parameters.

Upvotes: 0

Views: 89

Answers (2)

Duncan C
Duncan C

Reputation: 131436

The documentation for the description of the name parameter sounds like it was copy-and-pasted from the description of object.

Notifications are sent with a string notification name:

[[NSNotificationCenter defaultCenter] postNotificationName: @"somethingHappened" 
  object: self];

Usually, but not always, the "object" in the post notification call is the object that is posting the notification. It might also be the object that the notification is about:

[[NSNotificationCenter defaultCenter] postNotificationName: @"aHouseCaughtFire" 
  object: theHouseThatCaughtFire];

That post notification call sends a message (e.g. "aHouseCaughtFire"), and the object parameter tells what object the event was associated with.

When you register for a notification, you can say that you care about a specific notification string (name), a particular object, or both. As the other poster said, if you pass in a nil notification name and a non-nil object, you'll be notified about all notifications sent with the object parameter you specified.

If you are going on vacation, you might want to register about all notifications regarding your house. (notification = nil, object = your house.)

Thus, you'd get "aHouseCaughtFire" notifications, "aHouseWasRobbed" notifications, and "aHouseGotaAPackage" notifications about your house, but not notifications about other houses.

If you are a fire department, you might want to register for all "aHouseCaughtFire" notifications, regardless of which house it is. Then the notification handler would look up the address of the specific house, and dispatch a fire truck.

Upvotes: 1

Abizern
Abizern

Reputation: 150665

nil name and valid object - all notifications from that object will be passed to the block.

Valid name and nil object, all notifications of that name by any object will be passed to the block.

Valid name and valid object - notifications of that name by that object will be passed to the block.

The name of the notification depends on what notification you want to observe, we can't tell you what to use from the description of your problem as it stands.

Upvotes: 2

Related Questions