-2

After logging out the user, I use the code below after logging in again. The problem is that the application starts duplicated, all messages are duplicated. How to prevent it?

let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "view_controller_phone")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
UIView.transition(with: appDelegate.window!, duration: 1.0, options: .transitionCrossDissolve, animations: nil, completion: nil)

1 Answers1

0

It's a bit difficult to answer you without seeing what are you doing. But I'll give you 2 tips for look around ;)

First, check that your UIViewController are dismissed (it's your responsibility on doing that as Apple says) BEFORE you set the rootController again. A little trick is this:

https://stackoverflow.com/a/50333715/12277616

Second, take care on the observers if you have used them along with Notification Center. If this is the case I suggest you to remove the observers every time you dismiss a UIViewController so you are sure that they don't exist anymore in your runtime foreground.

Remember that dismiss(animated:true) can be closurable without parameters like this:

self.dismiss(animated:true) {

//Code to be executed when dismissing the controller
//Remove the observer

NotificationCenter.default.removeObserver(self, name: [observername],object:nil)

}
Massimo Pavanel
  • 754
  • 8
  • 7