调用两次addAuthStateDidChangeListener

时间:2022-02-02 20:05:05

I'm working with Firebase and per their docs, I'm checking to see whether or not a user's logged in state changes. The problem resides in the listener block, where the code is being called twice when the apps launched (user's logged in). It's not too much of a deal, but Firebase is creating two values in connections and removing both. How would I fix this issue? I'd need to call it after the user is obtained in the listener though and not outside because there is no guarantee that user will exist outside that block if it's not finished getting the user data first.

我正在使用Firebase和他们的文档,检查用户是否登录了状态更改。问题存在于监听器块中,当应用程序启动(用户登录)时,该代码将被调用两次。这没什么大不了的,但是Firebase在连接中创建了两个值,并删除了这两个值。我该如何解决这个问题?我需要在监听器中获取用户之后调用它,而不是在外部调用,因为如果用户没有首先获取用户数据,就不能保证用户会在该块之外存在。

FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
        if let theUser = user {
            // User is signed in.

            // CODE IN HERE IS CALLED TWICE UPON APP LAUNCH (WHEN USERS LOGGED IN).... WHY?
            self.currentUser = theUser
            print("LOGGED IN!!!!")


            self.updateUsersStatus(user: self.currentUser)

        } else {
            // No user is signed in.
            self.performSegueWithIdentifier(SEGUE_LOG_IN, sender: self)
        }
    }

2 个解决方案

#1


6  

+ click or option + click on the addAuthStateDidChangeListener function. You will see the following:

⌘+点击或选项+点击addAuthStateDidChangeListener函数。你会看到以下内容:

Registers a block as an "auth state did change" listener. To be invoked when:
    - The block is registered as a listener,
    - The current user changes, or,
    - The current user's access token changes.

To summarize - the block get's called immediately and then most likely gets called again after verifying the FIRApp and/or FIRAuth objects do not contain any stale data. So the behavior of invoking the callback twice should be expected and managed appropriately.

总之,在验证了FIRApp和/或FIRAuth对象不包含任何过期数据之后,立即调用block get,然后很可能再次调用。因此,应该对两次调用回调的行为进行预期并进行适当的管理。

#2


8  

I ran into the same issues as reported by Jamie22. Strange thing - I got two similar app setups, but only one app calls the auth state listener method twice on change.

我遇到了Jamie22报道的同样的问题。奇怪的是,我有两个类似的应用程序设置,但只有一个应用程序在改变时两次调用auth state listener方法。

This workaround is what solved the issue for me:

这个变通方案解决了我的问题:

var activeUser:FIRUser!

override func viewDidLoad() {
    super.viewDidLoad()

    FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth:FIRAuth, user:FIRUser?) in
        if let user = user {
            if(self.activeUser != user){
                self.activeUser = user
                print("-> LOGGED IN AS \(user.email)")
            }
        } else {
            print("-> NOT LOGGED IN")
        }
    })
}

By checking if the appUser has changed on auth state change, you can get rid of the second call, because it will be equal to the user you get in the first call.

通过检查appUser是否在auth状态更改时发生了更改,您可以删除第二个调用,因为它将等于您在第一个调用中获得的用户。

#1


6  

+ click or option + click on the addAuthStateDidChangeListener function. You will see the following:

⌘+点击或选项+点击addAuthStateDidChangeListener函数。你会看到以下内容:

Registers a block as an "auth state did change" listener. To be invoked when:
    - The block is registered as a listener,
    - The current user changes, or,
    - The current user's access token changes.

To summarize - the block get's called immediately and then most likely gets called again after verifying the FIRApp and/or FIRAuth objects do not contain any stale data. So the behavior of invoking the callback twice should be expected and managed appropriately.

总之,在验证了FIRApp和/或FIRAuth对象不包含任何过期数据之后,立即调用block get,然后很可能再次调用。因此,应该对两次调用回调的行为进行预期并进行适当的管理。

#2


8  

I ran into the same issues as reported by Jamie22. Strange thing - I got two similar app setups, but only one app calls the auth state listener method twice on change.

我遇到了Jamie22报道的同样的问题。奇怪的是,我有两个类似的应用程序设置,但只有一个应用程序在改变时两次调用auth state listener方法。

This workaround is what solved the issue for me:

这个变通方案解决了我的问题:

var activeUser:FIRUser!

override func viewDidLoad() {
    super.viewDidLoad()

    FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth:FIRAuth, user:FIRUser?) in
        if let user = user {
            if(self.activeUser != user){
                self.activeUser = user
                print("-> LOGGED IN AS \(user.email)")
            }
        } else {
            print("-> NOT LOGGED IN")
        }
    })
}

By checking if the appUser has changed on auth state change, you can get rid of the second call, because it will be equal to the user you get in the first call.

通过检查appUser是否在auth状态更改时发生了更改,您可以删除第二个调用,因为它将等于您在第一个调用中获得的用户。