So my goal is to be able to show the right vc without any bugs. There's actually multiple bugs with this issue. First it would make sense to show the block of code and explain it first so you can get some context on what I'm talking about.
In my SceneDelegate.swift
willConnectTo()
method, I have this block of code ...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
self.window = self.window ?? UIWindow()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let auth = Auth.auth()
let actualuser = auth.currentUser
auth.addStateDidChangeListener { (_, user) in
if let user = user {
db.document("student_users/\(actualuser?.uid)").getDocument { (docSnapshot, error) in
if error != nil {
print("\(error)")
} else {
guard let docSnap = docSnapshot?.exists else { return }
if docSnap == true {
let alreadyLoggedInAsAStudentViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
self.window!.rootViewController = navigationizedVC
self.window!.makeKeyAndVisible()
} else {
let alreadyLoggedInAsASchoolViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.SchoolEventDashboardStoryboardID) as! SchoolTableViewController
let navigationizedSchoolVC = UINavigationController(rootViewController: alreadyLoggedInAsASchoolViewController)
self.window!.rootViewController = navigationizedSchoolVC
self.window!.makeKeyAndVisible()
}
}
}
}
}
guard let _ = (scene as? UIWindowScene) else { return }
}
So in my app I have two types of users, and I used some logic to instantiate the proper viewController. The issue is this, say I log in as a student, minimize the simulator window, and rerun the simulator, it shows the wrong vc, but when I do the same thing again it shows the correct vc. The same thing applies for the other user. Also another bug, say the user is logged out and the state listener is not active, but as soon as I log in as a school user, the vcs are switched again, but if I rerun the simulator it'll obviously show the right vc.
I also get this error in the console, I looked it up, but I couldn't take what I seen in other issues and apply it to mine.
Here is a gif of the issue. You can see how the vc is wrong when I log in as a school user, but when I minimize simulator window and rerun it, the right vc shows up. Just wanna know how to prevent this. Thanks