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?
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
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the
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
The object posting the notification.
so in second case this tell who is actually sending this notification..that is that class itself.