why is my swift code executing out of order?

FirebaseAuth.Auth.auth().signIn is asynchronous – it runs in the background and not on the main thread. This means that signIn will be called some time in the future.

A solution is to put the call to getCurrentUser in the completion block:

func loginUser(email: String, password: String, viewModel: UsersViewModel) {
    FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password, completion: { result, error in
        guard error == nil else {
            print("error: \(error!)")
            return
        }
        print("user signed in")
        self.user = self.getCurrentUser(viewModel: viewModel) // move here
    })
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top