Right now the API doesn't seem to provide a way to detect if an observer has already been added for a particular NSNotification. What's the best way to avoid adding multiple NSNotification observers other than maintaining a flag on your end to keep track? Has anyone already created a category to facilitate this?
One way to prevent duplicate observers from being added is to explicitly call removeObserver for the target / selector before adding it again. I imagine you can add this as a category method:
@interface NSNotificationCenter (UniqueNotif)
- (void)addUniqueObserver:(id)observer selector:(SEL)selector name:(NSString *)name object:(id)object {
[[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:object];
[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:object];
}
@end
This assumes that the you will only add one unique observer to each target for any notification name, as it will remove any existing observers for that notification name.
addObserver...
indicates the object that will receive a message when the notification is posted, not the method that will make up that message. There's no notion of "target" in notifications. The thing that you're calling a "target" is referred to in the docs as "observer". –
Elaineelam Swift 5:
import Foundation
extension NotificationCenter {
func setObserver(_ observer: Any, selector: Selector, name: Notification.Name, object: Any?) {
removeObserver(observer, name: name, object: object)
addObserver(observer, selector: selector, name: name, object: object)
}
}
Swift 3-4:
import Foundation
extension NotificationCenter {
func setObserver(_ observer: AnyObject, selector: Selector, name: NSNotification.Name, object: AnyObject?) {
removeObserver(observer, name: name, object: object)
addObserver(observer, selector: selector, name: name, object: object)
}
}
Swift 2:
import Foundation
extension NSNotificationCenter {
func setObserver(observer: AnyObject, selector: Selector, name: String?, object: AnyObject?) {
removeObserver(observer, name: name, object: object)
addObserver(observer, selector: selector, name: name, object: object)
}
}
removeObserver(self, forKeyPath: tag)
? Where did you find this? –
Intermolecular The Upvoted answer with extension NotificationCenter { ... }
did not work for me, since my app was creating a new instance of a viewController (this had a Notification observer) every time a notification was posted, so removing an observer on a new instance of a viewController obviously doesn't work.
Previous instances of the viewController that had Notification Observers were getting called.
The below worked for me, since this was removing the Notification Observer as soon as the view was being disappeared.
// Notification observer added
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.someFunc(notification:)), name: Notification.Name("myNotification"), object: nil)
}
// Notification observer removed
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: Notification.Name("myNotification"), object: nil)
}
viewDidAppear
instead of viewWillAppear
. –
Intermolecular viewWillAppear
(maybe for animation only), it has to be placed in the function viewDidAppear
, when the view controller is appeared and it's ready to interact with a user. –
Intermolecular Well should have checked apple documentation.
https://developer.apple.com/documentation/foundation/notificationcenter/1411723-addobserver
let center = NSNotificationCenter.defaultCenter
var tokenOpt: NSObjectProtocol?
tokenOpt = center.addObserverForName("OneTimeNotification", object: nil, queue: nil) { (note) in
print("Received the notification!")
center.removeObserver(token!)
}
So to make sure that the notification is not add if it already existing
if let token = tokenOpt{
center.removeObserver(token)
}
tokenOpt = center.addObserverForName("OneTimeNotification", object: nil, queue: mainQueue) { (note) in
print("Received the notification!")
}
Swift 5:
A couple of tweaks for Swift 5 based on previous answers from @futureelite7 and @dimpiax
extension NotificationCenter {
func setObserver(_ observer: AnyObject, selector: Selector, name: NSNotification.Name, object: AnyObject?) {
NotificationCenter.default.removeObserver(observer,
name: name,
object: object)
NotificationCenter.default.addObserver(observer,
selector: selector,
name: name,
object: object)
}
}
© 2022 - 2024 — McMap. All rights reserved.