0

I have an application where there are two kinds of users. So I check if a user is logged in or not. If the user is logged in, I see what kind of user it is. Else, I go to the log in page. However, the application crashes, and I think it is because the Firebase requests takes too long to request.

Here is the code:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)

    window?.makeKeyAndVisible()
    if (FIRAuth.auth()?.currentUser) != nil {
        print("user is logged in...")

        var ref: FIRDatabaseReference!
        ref = FIRDatabase.database().reference()
        let userID = FIRAuth.auth()?.currentUser?.uid

        ref.child("patients").child(userID!).observe(.value, with: { (snapshot) in
            if snapshot.exists(){
                print("ahhhhh")
                self.window?.rootViewController = UINavigationController(rootViewController: PatientController())
            }
            else {
                print("doesn't exist...")
                self.window?.rootViewController = UINavigationController(rootViewController: PhysicianController())

            }
        })
    }
    else{
        print("hello?3")
        window?.rootViewController = UINavigationController(rootViewController: LoginController())
    }
    return true
}

Any Help will be greatly appreciated!

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
HDev
  • 29
  • 3
  • 3
  • 1
    any crash info from debugger log? **ref.child("patients").child(userID!)** // userID could be nil here!! – Sai Li Jan 12 '17 at 07:09
  • why is this code _even_ in the AppDelegate class?! this is a massively improper concept. – holex Jan 12 '17 at 08:37
  • Your `AppDelegate` should construct a view controller and assign it as the rootViewController immediately. You should move your authentication logic to inside of that view controller (and preferably move it off the main thread). – sdasdadas Jan 12 '17 at 11:58

0 Answers0