这个firestore关闭会导致内存泄漏吗? [英] is this firestore closure causing a memory leak?

查看:55
本文介绍了这个firestore关闭会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的目标是在实例化正确的 viewController 时解决这个条件问题.我有一个函数,我基本上用它来根据用户的类型以及他们是否登录将用户导航到正确的 viewController.

So my goal is to fix this condition issue when it comes to instantiating the right viewController. I have a function that I basically use to navigate a user to the right viewController depending on the type of user and if they're logged in or not.

这是这个函数:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    let window = UIWindow(windowScene: windowScene)
    self.window = window

    let auth = Auth.auth()
    let actualuser = Auth.auth().currentUser


    auth.addStateDidChangeListener { (_, user) in
        if user != nil {

            db.document("student_users/\(actualuser?.uid)").getDocument { (docSnapshot, error) in
                if error != nil {
                    print("\(error)")
                } else {
                    let docSnap = docSnapshot?.exists
                    guard docSnap! else {
                        let alreadyLoggedInAsASchoolViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.SchoolEventDashboardStoryboardID) as! SchoolTableViewController
                        let navigationizedSchoolVC = UINavigationController(rootViewController: alreadyLoggedInAsASchoolViewController)
                        self.window!.rootViewController = navigationizedSchoolVC
                        self.window!.makeKeyAndVisible()
                        return
                    }
                        let alreadyLoggedInAsAStudentViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
                        let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
                        self.window!.rootViewController = navigationizedVC
                        self.window!.makeKeyAndVisible()
                 
                }
            }
        } else {
            let notLoggedInAtAll = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.GothereMainMenuStoryboardID) as! GothereMainMenuViewController
            let navMainMenu = UINavigationController(rootViewController: notLoggedInAtAll)
            self.window!.rootViewController = navMainMenu
            self.window!.makeKeyAndVisible()
        }
    }
}

对于 sceneDidEnterForeground,我也有这样的确切代码块,用于推送通知.现在的问题是当我第一次运行模拟器并启动应用程序时,会显示正确的 viewController,但是当我以学校用户的身份注销并在同一个模拟器会话中以学校用户的身份登录时,错误的 viewController(又名其他类型用户的视图控制器)出现.

I also have the exact block of code like this for the sceneDidEnterForeground for push notification purposes. Now the issue is when I run the simulator and launch the app for the first time, the correct viewController will show up, but when I logout as a school user and login as a school user in that same simulator session, the wrong viewController (aka the viewController of the other type of user) shows up.

这并不是生产中的真实情况,即学生用户只能访问学校用户的帐户并在同一场景会话中像这样登录,但仍然安全总比抱歉好.所以这导致我问,这是内存泄漏还是完全不同的问题?

Not that it would be a real situation in production where a student user would just have access to a school user's account and log in like that in the same scene session, but still, better to be safe than sorry. So this leads to me ask, is this a memory leak or a completely different issue?

我也收到此错误:

推荐答案

您的查询基于变量 actualuser,它看起来只在第一次设置场景时设置一次.在状态更改回调中,它永远不会更新.

Your query is based on the variable actualuser, which looks like it is only set once, when the scene is first set up. Inside the state change callback, it's never updated.

因此,当您注销,然后以不同的用户身份重新登录时,将使用 actualuser 的初始值,解释为什么您看到错误的视图控制器.然后,当您再次运行应用程序并设置场景时,actualuser 再次设置为 auth().currentUser,向您显示正确的视图控制器.

So, when you log out, then log back in as a different user, that initial value of actualuser will be used, explaining why you see the wrong view controller. Then, when you run the app again and the scene is set up, actualuser gets set to the auth().currentUser again, showing you the correct view controller.

这里的解决方案是基于当前(和当前)用户进行查询.

The solution here is to base your query on the current (and current) user.

类似于:

 db.document("student_users/\(user.uid)")

(而不是检查 user != nil,使用 let user = user 做一个可选绑定,然后你可以避免 ? 解包)

(Instead of checking user != nil, do an optional binding with let user = user and then you can avoid the ? unwrapping)

顺便说一下,这不是内存泄漏,这是一种不同类型的问题:https://en.wikipedia.org/wiki/Memory_leak

This is not, by the way, a memory leak, which is a different type of issue: https://en.wikipedia.org/wiki/Memory_leak

这篇关于这个firestore关闭会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆