What's Difference between NSNotification 'object' and 'userInfo'? - ios

What's difference between NSNotification's object and userInfo?
When I post a notification with a parameter, I can use object or userInfos to do it. But I do not know what the difference is between these two ways.
Is there some advantages to using userInfo? Or is using object enough?

The object represent the object which posted the notification. userInfo contains the additional information/data for the receiving object/function.
According to NSNotificationCenter Class Reference:
postNotificationName:object:userInfo:
Creates a notification with a given name, sender, and information and
posts it to the receiver.
Declaration
Swift
func postNotificationName(_ notificationName: String, object notificationSender: AnyObject?, userInfo userInfo: [NSObject : AnyObject]?)
Objective-C
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
Parameters
notificationName
The name of the notification.
notificationSender
The object posting the notification.
userInfo
Information about the the notification. May be nil.
Discussion
This method is the preferred method for posting notifications.

If you define an object you can filter the notifications sent only by that object. For instance if you register for a notification, specifying an object as notificationSender you get notification only from that object even if the notification name is the same for other posted notifications:
- (void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender
Here is from Apple doc:
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.

When working with an NSNotification object, you’ll want to familiarize yourself the userInfo dictionary, which provides access to any additional objects that may be of interest to the receiver. Understanding the object method may also be helpful if you are using the same notification on more than one object.
For Further detail go thtough this link.
http://iosdevelopertips.com/cocoa/nsnotification-userinfo-and-object-methods.html

Related

What does post to notification center mean

What is to post to notification center mean?
What is the difference between to post and to add observer using NotificationCenter.
NSNotificationCenter : NSNotificationCenter can be consider as an interface used for communicating information within your app.Unlike push or local notifications where you are notifying a user of any content you would like them to receive, NSNotificationCenter allows us to send and receive information between classes and/or structs based on an action that has occurred in our app. NotificationCenter simply can be thought of an broadcaster and we tune into some stations/channels to receive the changes if any.
NotificationCenter.default is where all the notifications are observed and posted.Each notification has a unique identifier which can be used to validate the channel at broadcasting end as well as receiving end.
addObserver() : Objects register with a notification center to receive notifications using the addObserver(_:selector:name:object:) or addObserver(forName:object:queue:using:) methods. When an object adds itself as an observer, it specifies which notifications it should receive. An object may therefore call this method several times in order to register itself as an observer for several different notifications.The class implementing the addobserver() method is reciever.
Example : Adding an observer(this is will at the receiving end)
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
#objc func methodOfReceivedNotification(notification: Notification){}
post() : Creates a notification with a given name and sender and posts it to the notification center. Creating a package and send it through the channel. The class implementing the post() method is broadcaster.
Example : Posting an observer(this is will at the broadcasting end)
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Note that the "NotificationIdentifier" is the unique name to identify the particular channel. And selector is method/action that need to be performed when a notification is received.You can also pass the data within the notification center within the "object" parameter.
"With respect to your question "What is the difference between to post and to add observer using NotificationCenter."
The answer is they both go head to head, one (add-observer()) is used to send and another one (post()) is used to receive. so if you are posting a notification its must that you should implement an observer too.In short if you throw something you need someone to catch, if you speak, you need someone to listen.
Simply notification center used to post information to registered observer
Information about NotificationCenter
Example

NS Notification Confused Swift 3

Currently in the process of implementing NS Notification in to my first IOS application. I am wanting to use it so when a user succesfully makes a purchase, the product name is broad cast out, and the listener runs the method to unlock the products. Looking at tutorials i am confused about how it works and would really appreciate if these could be cleared up. I will post the code below and what i think it does, if their are any mistakes please point these out.
Line 1 - Let is a variable that can not be changed, NotificationName is the name of the variable. Notfication.Name is declaring the name as "Notification Identifier"
let notificationName = Notification.Name("NotificationIdentifier")
Line 2 - I think here the "NotificationName" is the id of this for the listener as defined above by the let.
If i want to pass in an object is this done at the end? via the object tag. IE object: productsRequest
NotificationCenter.default.post(name: notificationName, object: nil)
Line - 3 This is the listener, the notification name has to match the post to receive the correct notification i assume. The add observer self selector is confusing me, and if i pass in an object from the post line 2 above, ie Object Products Request. Does this have to be declared in the listener?
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification), name: notificationName, object: nil)
Any explanations to further my understanding are appreciated. Sorry if these seems like a basic question, how ever reading tutorials i am struggling to understand certain elements of this.
Thanks
Correct.
Correct. The object you pass into postNotification method is a sender, which can be used to filter notifications with the same name (p.3)
When you subscribe for a specific notification and pass an object to addObserver method, you will receive only notifications which were sent with this exact object. If you don't specify it in addObserver you will receive all notifications with this name.
Don't forget to removeObserver as soon as you don't need to listen to them anymore. If your listener is a view controller, it is usually a good practice to addObserver in viewWillAppear and removeObserver in viewDidDisappear methods.

Clarifying the purpose of notificationSender in NSNotification's addObserver function

Could someone kindly clarify the purpose of notificationSender in the addObserver function of NSNotification?
Here's the explanation from the Apple docs:
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.
We use notifications to respond when a video has ended. The code:
NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
where playerItem contains the video in question. However, passing nil for object seems to have no discernible effect.
It's better for us to use nil instead of playerItem because we wouldn't need to create another class variable.
What's the risk in using nil, and what's the advantage in using playerItem?
The only reason to be specific with the notificationSender is if you have chosen a notification name that other objects (that you don't want to listen to) are going to send to you. Specifying a notificationSender is akin to a delegate relationship, where leaving it nil means that absolutely any object could send you a notification with the same String and you would process it.
In practice, this is rarely an issue as long as you pick notification strings that are unique. You'll see people using things like com.my.app.name.notification or kMyAppNotification for precisely this reason.
And as to the specific line of code you linked, I don't actually know the implementation details, but I'm assuming if you added a listener with the notificationSender property pointing to an object, you also need to remove the same listener with the notificationSender property set, or else risk a memory leak by not removing observers. But I'd have to read the docs again to figure that out.

NSNotificationCenter - removing observer when observed objected is deallocated

I use the following line to add an observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
My observer (self) will never be deallocated.
But when the user starts playing a different item, the old self.playerItem will be deallocated---but will always be replaced with a new one, which I want to continue observing.
When that happens, what happens in regards to my observer's status as an observer? Do I need to do something to stop observing the deallocated object, as is required with KVO? Or will I continue observing the new object at self.playerItem? Or will my observer automatically be "unregistered?"
If I need to remove the observer, I wonder why there's no corresponding removeObserver method that enables one to specify a selector; it seems I can remove an observer only wholesale via removeObserver:(id)notificationObserver.
According to the NSNotificationCenter class reference:
Be sure to invoke removeObserver: or removeObserver:name:object:
before notificationObserver or any object specified in
addObserver:selector:name:object: is deallocated.
So: you should unregister your observer before self.playerItem deallocated.
But when the user starts playing a different item, the old
self.playerItem will be deallocated---but will always be replaced with
a new one, which I want to continue observing.
You may pass nil as the last parameter of addObserver:selector:name:object: method:
Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
If you don't specify
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
So you will receive notification AVPlayerItemDidPlayToEndTimeNotification from any object that posts it.
Since iOS 9 it is no longer needed to remove an observer from an object:
In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated.
However, block based observers need to be un-registered as before:
Block based observers via the -[NSNotificationCenter addObserverForName:object:queue:usingBlock] method still need to be un-registered when no longer in use since the system still holds a strong reference to these observers.
More information can be found here:
https://developer.apple.com/library/content/releasenotes/Foundation/RN-FoundationOlderNotes/index.html#10_11NotificationCenter

Where and how do I register an object for receiving a Notification?

For example, when memory gets low, the System sends a UIApplicationDidReceiveMemoryWarningNotification notification. That's all Apple says in its docs at that point. But where does this notification come from, and to which method is it sent? Or where and how do I register what that I get notified?
From within the initialization code of the class you wish to receive the notification make the following method call:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleMemoryWarning:) name: UIApplicationDidReceiveMemoryWarningNotification object:nil];
This assumes that your class also implements a handleMemoryWarning method as follows:
- (void) handleMemoryWarning:(NSNotification *)notification
{
}
Much simpler to use the application delegate and implement the optional method
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
Most common notifications are also available translated into calls to a delegate, typically to optional methods in a formal protocol. Your delegate can be whatever object you like.
It is sent to the notification center, where all notifications are centralized. An object that wants to get informed about this notification registers itself to the notification center by telling which notification it wants to get informed and which method should be invoqued when the notification is raised.
For more information you can take a look to Notification programming topics for Cocoa and NSNotification class reference .
Be warned that your selector will need to take the notification as an argument.
If you use something like #selector(handleMemoryWarning) and - (void) handleMemoryWarning { } the object WILL NOT send the notification and you'll still be holding onto all of your memory.
I was just bitten by this.

Resources