Firebase Android onAuthStateChanged 调用了两次 [英] Firebase Android onAuthStateChanged called twice

查看:25
本文介绍了Firebase Android onAuthStateChanged 调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用新的 Firebase SDK.

I've start working with new Firebase SDK.

当我进行用户登录时,我的 onAuthStateChanged 方法以相同的状态被调用两次(等用户登录).

When I'm doing user login, I'm onAuthStateChanged method is being called twice with same state (etc. user sign in).

我确定我只将 AuthStateListener 添加到 FirebaseAuth 参考中一次.

I'm sure I'm adding the AuthStateListener only once to the FirebaseAuth reference.

有什么帮助吗?

推荐答案

是的,这很烦人.这是由于注册电话.不仅如此,onAuthStateChanged 将在许多不同的状态下被多次调用,不可能知道它是哪个状态.

Yes, and this is very annoying. This is due a registration call. Not only that, onAuthStateChanged is going to be called many times in many different states, with no possibility of knowing which state it is.

文档 说:

onAuthStateChanged(FirebaseAuth auth)

onAuthStateChanged(FirebaseAuth auth)

此方法在身份验证状态发生变化时在 UI 线程中调用:

This method gets invoked in the UI thread on changes in the authentication state:

  • 在注册监听器之后

当用户登录时

当前用户退出时

当前用户发生变化时

当前用户的token发生变化时

When there is a change in the current user's token

这里有一些发现当前状态的提示:

Here some tips to discover the current state:

  • 注册调用:跳过带有标志的第一个调用.
  • 用户登录:来自参数的用户是 != null.
  • 用户退出:来自参数的用户 == null.
  • 当前用户更改:来自参数的用户为 != null,最后一个用户 ID 为 != 来自参数的用户 ID
  • 用户令牌刷新:来自参数的用户是 != null 并且最后一个用户 ID 是 == 来自参数的用户 ID

这个听众一团糟,而且很容易出错.Firebase 团队应该调查一下.

This listener is a mess and very bugprone. Firebase team should look into it.

这篇关于Firebase Android onAuthStateChanged 调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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