1

I have implemented a Firebase Google login into my Swift App. I want to check in the beginning if its already a logged in user or not. I do this like that:

override func viewDidLoad() {
        super.viewDidLoad()
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance().delegate = self

    }

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

        if Auth.auth().currentUser != nil {

          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let vc = storyboard.instantiateViewController(withIdentifier: "feedVC")
          vc.modalPresentationStyle = .fullScreen
          self.present(vc, animated: false)

        }
    }

Now my problem is: It works how I want, but its pretty slow. I can still get a quick look at the login screen, before my main page gets presented.

Now if I want to add the currentUser check to the viewDidLoad, the App doesnt check if the User has already logged in before.

Any recommendations?

5
  • There's really nothing in this question that would be slow by by any means because you're not doing any Firebase network calls or working with anything outside of fast local data. What exactly is slow? Commented May 20, 2020 at 18:43
  • The code in your question is already running on the main thread so the code presented in the accepted answer doesn't really have any affect. However, there may be more to this so more info is needed to really provide an accurate answer. Commented May 20, 2020 at 18:50
  • @Jay On my actual iPhone I have no delay, but on the simulated device I somehow have small delays. I dont know why. Commented May 21, 2020 at 14:10
  • But in the end it doesn't matter that much, since it works on the real iPhone Commented May 21, 2020 at 14:10
  • My concern is you're going to run into bigger problems down the road so better to address it up front than wait for it to blow up later, and the answer provided isn't a long term fix as it doesn't do anything. It's not clear where you're getting delays so if you would like additional help, update the question with more info and we can take a look. Commented May 21, 2020 at 15:45

1 Answer 1

0

Do it on main thread

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

        if Auth.auth().currentUser != nil {
    DispatchQueue.main.async {
     let storyboard = UIStoryboard(name: "Main", bundle: nil)
              let vc = storyboard.instantiateViewController(withIdentifier: "feedVC")
              vc.modalPresentationStyle = .fullScreen
              self.present(vc, animated: false)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@janswoboda I'm confused by this answer. What's in the question is already running on the main thread so adding another DispatchQueue does nothing for this use case.
yeah you are right @Jay ... its not root cause ... it has root cause somewhere when he pushed this controller ...
sometimes i too face this problem ... shifting to main thread solves it ... doent know the reason ... thats why i propose this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.