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

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

问题描述

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

当我正在做用户登录时,我正在调用两次具有相同状态的onAuthStateChanged方法(etc 。



我确定我只将AuthStateListener添加到FirebaseAuth引用中。



有什么帮助吗?
解决方案

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

文档说:


onAuthStateChanged(FirebaseAuth auth)



这个方法在UI线程中被认证状态的变化调用:


  • 在侦听器注册后

  • 当用户已登录


  • 当前用户注销

  • 当前用户更改

  • 当前用户令牌发生变化时


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


  • 注册电话:跳过带有标志的第一个电话。

  • 用户用户注销:参数中的用户==为空。

  • 当前用户更改:用户from参数是!= null,最后一个用户id是!=用户id来自参数
  • 用户令牌刷新:用户从参数是!= null,最后一个用户ID是==参数中的用户ID



这个监听器是一团乱七八糟的事情。 Firebase团队应该查看它。

I've start working with new Firebase SDK.

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

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

Any help?

解决方案

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.

Documentation says:

onAuthStateChanged(FirebaseAuth auth)

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

  • 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

Here some tips to discover the current state:

  • Registration call: skip the first call with a flag.
  • User signed in: user from parameter is != null.
  • User signed out: user from parameter is == null.
  • Current user changes: user from parameter is != null and last user id is != user id from parameter
  • User token refresh: user from parameter is != null and last user id is == user id from parameter

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

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

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