身份验证状态更改侦听器被激发两次? [英] Auth state change listener being fired twice?
问题描述
if
语句检查被验证两次。这里是我的代码:pre $
override func viewDidAppear(_ animated:Bool){
FIRAuth.auth()?. addStateDidChangeListener {auth,user
if user = user {
print(User is logged in)
}
当页面加载而不是一次时,用户已登录会被打印两次到控制台。我正在做一些不正确的事情吗?
我面临同样的问题。而不是用于登录。它可以面对流动选项
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
-
- 当用户登录时
- 当前用户注销时
- 当前用户更改
- 当当前用户令牌发生变化时
所以只是一个标志,然后检查一个。或检查回应 nil
或不是
试试这个方法:
override func viewDidAppear(_ animated:Bool){
FIRAuth.auth()?. addStateDidChangeListener {auth,user in $ b $ if let user = user {
if user!= nil {
print(User is logged in)
}
}
注意:尝试使用标记
并检查 user =!零
When my page loads I need to know whether the user is logged in and perform different tasks based on that. The first thing I do is check if the they are logged in, but for some reason the if
statement to check that is being validated twice. Here is my code:
override func viewDidAppear(_ animated: Bool) {
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
print("User is logged in")
}
"User is logged in" gets printed out twice to the console when the page loads instead of just once. Am I doing something incorrectly?
i faced same problem. and it is not for logged in . it can be faced flowing option https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
- Right after the listener has been registered
- When a user is signed in
- When the current user is signed out
- When the current user changes
- When there is a change in the current user's token
so just crate a flag then check that one . or check response nil
or not
Try this way :
override func viewDidAppear(_ animated: Bool) {
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
if user != nil {
print("User is logged in")
}
}
Note : try to use both flag
and check user =! nil
这篇关于身份验证状态更改侦听器被激发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!