身份验证状态更改侦听器被激发两次? [英] Auth state change listener being fired twice?

查看:120
本文介绍了身份验证状态更改侦听器被激发两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的页面加载时,我需要知道用户是否登录,并根据这个执行不同的任务。我做的第一件事就是检查他们是否已经登录,但是由于某些原因, 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屋!

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