-2

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.

error

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

gif

dsonawave
  • 137
  • 2
  • 12

1 Answers1

0

you need to start your window like this:

guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window

instantiating the UIWindow is recommended only when using appDelegate without the sceneDelegate, so that could be causing memory issues

Also, keep in mind that you are calling a document before opening your app. So check if the variable docSnap isn't returning false on the first time. maybe that's it. But in your code isn't clear which view is which

  • Ok ok, so couple questions for you. First question, would me instantiating the window in the appDelegate prevent the memory issues? Next question, how can I check if the `docSnap` variable is returning false the first time? Last question, what part of my code isn't clear?, i'd be more than glad to explain it more if you're willing to help. @Felipe Andrade – dsonawave Feb 28 '21 at 19:08
  • Also, it most likely is returning false the first time if it's showing the wrong view, so how can I prevent this?, I thought a guard statement would get the job done, but guess I was wrong. @Felipe Andrade – dsonawave Feb 28 '21 at 19:15
  • the best way for you to check what is happening is with a breakpoint. if you click on the number of the line on the file it will stop right there during run. So after that you can call on the log window `po {my variable}` to know what is happening. and also you can run some code there – Felipe Andrade Feb 28 '21 at 21:23
  • I am not sure if going to appDelegate will fix the issue, but it is worth a try. You code is good for a working code, but I would recommend you to read something on the internet about `clean code` – Felipe Andrade Feb 28 '21 at 21:24