从 SceneDelegate 获取 viewController 实例化的错误

问题描述

所以我的目标是能够在没有任何错误的情况下显示正确的 vc。这个问题实际上有多个错误。首先,展示代码块并首先对其进行解释是有意义的,这样您就可以了解我在说什么。

在我的 SceneDelegate.swift willConnectTo() 方法中,我有这段代码......

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 }
}

所以在我的应用程序中,我有两种类型的用户,我使用了一些逻辑来实例化正确的 viewController。问题是这样的,假设我以学生身份登录,最小化模拟器窗口,然后重新运行模拟器,它显示了错误的 vc,但是当我再次做同样的事情时,它显示了正确的 vc。同样的事情适用于其他用户。还有另一个错误,比如用户已注销并且状态侦听器未处于活动状态,但是一旦我以学校用户身份登录,vcs 就会再次切换,但是如果我重新运行模拟器,它显然会显示正确的 vc .

我在控制台中也遇到了这个错误,我查了一下,但我无法将我在其他问题中看到的内容应用到我的问题中。

error

这是问题的 gif。当我以学校用户身份登录时,您可以看到 vc 是如何出错的,但是当我最小化模拟器窗口并重新运行它时,正确的 vc 出现了。只是想知道如何防止这种情况。谢谢

gif

解决方法

你需要像这样启动你的窗口:

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

仅在不使用sceneDelegate 的情况下使用appDelegate 时才推荐实例化UIWindow,这样可能会导致内存问题

另外,请记住,您在打开应用之前正在调用文档。因此,请检查变量 docSnap 是否第一次没有返回 false。也许就是这样。但是在您的代码中不清楚哪个视图是哪个

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...