Can someone explain the object part in a push notification code?

I came across a piece of code that was registering a notification with:

 [[NSNotificationCenter  defaultCenter] addObserver:self
                                              selector:@selector(someStuff:)
                                                  name:@"someStuff"
                                                object:nil];

and then triggerring it with:

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

Why is one object set to nil and other to self? What does that do?

Answers


Taking the second case first, when you post a notification you indicate which object is the sender of that notification. Usually that is 'self', but you can post a notification on some other object's behalf.

On the other hand, when you register to observe notifications, you can specify that you only want to observe notifications that are posted by a particular object. So you can say "I want to observer the 'cool new data' notification, but only if this particular object posts it. If some other object says there is 'cool new data', I'm not interested."

If you pass nil as the 'object' parameter when you register an observer, then you're saying you don't care who posts this notification, you want your selector (or block) to fire for any sender. So if anyone posts 'cool new data', I want to know about it.

BTW, these are not "push notifications". That's a different feature/API.


to quote apple docs.. the first object is

notificationSender

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.

and the second is

notificationSender

The object posting the notification.

so in second case this tell who is actually sending this notification..that is that class itself.


Need Your Help

copy production database to staging capistrano

ruby-on-rails capistrano production staging

I am using rails and capistrano with a staging and production server. I need to be able to copy the production database to the staging database when I deploy to staging. Is there an easy way to