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.
Related
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
Are the new notifications not currently working in Swift 3?
I am doing:
NotificationCenter.default().post(name: DidTouchParticleView, object: self.particle as? AnyObject)
In a custom view's touchesBegan() and I need to send the particle object to the view controller if there is one. So I do this:
NotificationCenter.default().addObserver(forName: DidTouchParticleView,
object: self,
queue: OperationQueue.main(),
using: presentParticleDisplayView(notification:))
In a view controller's viewDidLoad(). I am certain that that particular view controller is the one presented when I tap my custom view, however, the function presentParticleDisplayView(notification:) is never called.
Also, DidTouchParticleView is defined globally like this:
let DidTouchParticleView = NSNotification.Name("didTouchParticleView")
Is this due to the beta, or am I doing something wrong?
It sounds like you may be intending to call addObserver(_:selector:name:object:), where the second parameter message (the selector:) is sent to the first parameter (the target).
Instead, you are calling the wrong method, addObserver(forName:object:queue:using:), which works quite differently.
Also, as to the second part of your question:
let DidTouchParticleView = NSNotification.Name("didTouchParticleView")
That is correct (almost); it should be
let DidTouchParticleView = Notification.Name("didTouchParticleView")
All notification names are now Notification.Name instances. The proper way to do this is to say:
extension Notification.Name {
static let didTouchParticleView = Notification.Name("didTouchParticleView")
}
You can then refer to the notification's name as .didTouchParticleView throughout your code.
You're watching for yourself to post the notification (object: self). You probably mean something like object: particle or possibly object: nil in the addObserver call (but then be careful to make sure it's the right particle in the handler). Re-read the docs for this method and note carefully what the object parameter is for.
Note that this version of addObserver returns an observer object. You must store that so you can later call removeObserver with it.
Is there a way to see if an object is already an observer for a type of notification?
Currently, each addObserver call is paired with removeObserver to avoid duplicate observers, but is there a way to see if an object is already an observer before invoking addObserver?
NSNotificationCenter.defaultCenter().removeObserver(self, name: CustomEvent, object: foo)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #("test"), name: CustomEvent, object: foo)
Unfortunately no, there is not. Just like KVO, notification center doesn't provide an API that lets us check whether an object (self in this case) has already been registered as an observer or not.
You will have to yourself keep track using an bool variable and set it to "true" when you make an addObserver call and reset it when you call removeObserver.
Call addObserver again only when the bool is set to "false".
There is no other way to figure out if object is already an observer.
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.
I'd like to retrieve a list of observers (objects and selectors) for a given notification name. I know there's no official API for that. I also know I could subclass NSNotificationCenter to accomplish this. Sometimes however this is not a viable option because NSNotificationCenter usage is spread all over the code or even binary frameworks.
So I'm looking for an unofficial/private way to do this. (Since it's about debugging only, that's fine.)
Finally, Apple added a way to print all notification center observers:
po [NSNotificationCenter defaultCenter]
It prints a comma separated list with Name, Object, Observer, and Options:
<NSNotificationCenter:0x7f997b307500>
Name, Object, Observer, Options
WebPreferencesRemovedNotification, 0x11165b680, 0x116c87ff8, 1400
UIApplicationWillEnterForegroundNotification, 0x11165b680, 0x7f997a838000, 1400
...
If you don't want to subclass NSNotificationCenter you can rename original addObserver:selector:name:object method and create your own with such name and add observers in there to some array then call original renamed method.
Take a look at following methods: class_addMethod, class_replaceMethod, class_getMethodImplementation.
Also look at this SO question: Method Swizzling
I am not sure why you want observers but you might find this class useful, which removes observers automatically for you which I think might be what you want. SFObservers