为什么使用Firebase身份验证方法调用模式? [英] Why is the Firebase authentication method call patttern used?

查看:44
本文介绍了为什么使用Firebase身份验证方法调用模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用Firebase身份验证模式

Why do the Firebase authentication patterns use

auth = FirebaseAuth.getInstance();
auth.<signin type>.addOnCompleteListener

不是在尝试进行身份验证之前将onCompleteListener添加到auth对象?如果不使用,就没有比赛条件:

Instead of adding the onCompleteListener to the auth object before making the authentication attempt? Isn't there a race condition if one does not use:

auth.addOnCompleteListener(...)
auth.<signon attempt>

推荐答案

开始登录流程时,它将返回任务.身份验证流完成时(成功或有错误),该任务即完成.这是一个类似于Promise的API,这意味着您可以延迟添加完成侦听器,并且仍会被调用.

When you start a sign-in flow, it returns a task. That task completes when the auth flow completes (either successfully or with an error). It's a promise-like API, which means that you can add the completion listener late and it will still be invoked.

我更喜欢自己使用 AuthStateListener :

auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        setCurrentUser(firebaseAuth.getCurrentUser());
    }
});

将使用当前身份验证状态立即调用 onAuthStateChanged()方法,然后在身份验证状态更改时立即调用.这消除了我对竞赛条件的任何恐惧,并使我的代码非常活跃".

The onAuthStateChanged() method will be called immediate with the current auth state and then whenever the auth state changes. This removes any fear for race conditions from my mind and makes my code very "reactive".

我认为我们在内部将这些时变值称为值",但不确定是否这也是它们的更通用术语.

I think we internally called these time-varying values, but am not sure if that's also the more general term for them.

这篇关于为什么使用Firebase身份验证方法调用模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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