I have an app which utilizes a NotificationCenter
to call a function in viewControllerA
when a button is pressed in viewControllerB
. It works as intended, kind of. Here is the general setup:
ViewControllerA
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadMainPageVC), name: NSNotification.Name(rawValue: NotificationName.reloadingMainPageVC), object: nil)
}
func reloadMainPageVC(){
//appends some arrays
self.tableView.reloadData()
print("we reloaded viewControllerA")
}
These functions allow for the reloading of viewControllerA
's tableview. It can be called by a button press from a button located in viewControllerB
.
ViewControllerB
func postNotificationtoReloadMainPageVC() {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationName.reloadingMainPageVC), object: nil)
}
@IBAction func buttonTap(_ sender: UIButton) {
postNotificationReloadMainPageVC()
}
This works in the sense that pressing the button in viewControllerB
does indeed reload the table in viewControllerA
. Hitting this button the first time reloads viewControllerA
and the log reads we reloaded viewControllerA
. I now clear the log. Hitting this button again (the second time) will now result in the viewControllerA
being reloaded twice (and this causes some duplicates to show up in the table due to some asynchronous appending...) and the log now reads we reloaded viewControllerA we reloaded viewControllerA
. I clear the log again. Hitting the button a third time results in viewControllerA
being reloaded three times and the log now reads we reloaded viewControllerA we reloaded viewControllerA we reloaded viewControllerA
.
The Problem
In case it wasn't obvious, the issue is that regardless of the amount of times I have hit the button, each button hit should result in exactly one reload of the viewControllerA
's tableview. However, the 2nd time hitting the button does it twice, the 3rd three times, etc...
What I think the cause is
I do not exactly understand the underlying mechanism of the NotificationCenter
, but what my guess (and this is a straight up guess) is, is that the notification is being "posted" every time the button is hit, but any notifications that are already there before the button is hit, is run in addition to the new notification being posted. ie the first time the button is hit, there is no prexisting notification, so it is run once. 0 + 1 = 1. The second time, though, there is already an existing notification, so hitting the button calls 1 (the previous) + 1 (the new) = 2... so it is run twice.
Or maybe that's totally wrong and it is something else?
What I think the solution may be
If my guess at the root of the problem is correct, this can be solved by adding a removal of previously existing notifications from the NotificationCenter
:
@IBAction func buttonTap(_ sender: UIButton) {
//remove previous notifications
postNotificationReloadMainPageVC()
}
The issue is that I have no idea what the syntax for that would be. Again, it is totally possible that my assumption was wrong too... in which case... what is the issue and how do I fix it?