Thread 1: signal SIGABRT caused because of a switch storyboard function with Firebase

This is my code:

Auth.auth().addStateDidChangeListener() { auth, user in
    if user != nil {
        self.switchStoryboard()
    }

that one is inside of

override func viewDidLoad() { 
    super.viewDidLoad()

and then this is outside the viewDidLoad:

func switchStoryboard() {
    let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! ViewController
    self.navigationController?.pushViewController(loginVC, animated: true)
}

I really don't know why it's crashing.

My Login view controller is called LoginViewController.

1 answer

  • answered 2018-07-11 03:44 Linh Ta

    Try to present the LoginViewController instead of adding it to the navigationController, once the user logs in you can dismiss it

    let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! ViewController
    self.present(loginVC, animated: true, completion: nil)
    

    If you want to use navController, makes sure you set the rootViewController first before performing any action.

    If that's not the case, it's might be because addStateDidChangeListener func. You can try to perform the pushing on the Main Queue:

    DispatchQueue.main.async {
        let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! ViewController
        self.navigationController?.pushViewController(loginVC, animated: true)
    }