Manny Manny - 6 days ago 5
Swift Question

ViewController is appearing 2-5 times on Push, login screen using Firebase (video)

YOUTUBE VIDEO OF PROBLEM

RootViewController - (Home Screen) - This is the very first screen, Firebase then detects if a user is already logged in on startup or they just signed up and then it logs them in.

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

FIRAuth.auth()!.addStateDidChangeListener() { auth, user in

if user != nil {
self.performSegue(withIdentifier: "home", sender: nil)
}
}
}


This is how I'm dismissing controllers and the same action that gets triggered when signing out a user to return to the home screen:

_ = navigationController?.popViewController(animated: true);
dismiss(animated: true, completion: nil)


If you see the video link, that's what's happening, and I don't know why or what is happening.

Answer

Your FIRAuth.auth()!.addStateDidChangeListener() is getting called multiple times when you are logging in with a user and its the reason why the view is getting pushed multiple times.

Would there be a chance to change your code to this?

if let user = FIRAuth.auth()?.currentUser {
  // User is signed in.
  self.performSegue(withIdentifier: "home", sender: nil)
} else {
  // No user is signed in.
  // ...
}