Firebase单一值事件侦听器不会返回 [英] Firebase single value event listener doesn't return

查看:172
本文介绍了Firebase单一值事件侦听器不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序中成功使用了Firebase数月。我刚刚完成更新使用9.0.0版本,我还有一个问题,我无法弄清楚。

在我的MainActivity onCreate函数中,我检查用户是否登录到Firebase。如果用户登录,那么我试图获取我在我的数据库中的用户对象。

这是应用程序中唯一的实例麻烦与此。我已验证用户已登录,并且DatabaseReference指向正确的url。



创建DatabaseReference并添加侦听器,但侦听器上没有任何事情触发...

  FirebaseAuth auth = FirebaseAuth.getInstance(); ($ auth!= null&& auth.getCurrentUser()!= null){
DatabaseReference userRef = FirebaseDatabase.getInstance()。getReference()。child(USERS)。

。孩子(auth.getCurrentUser()的getuid());
userRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//成功的逻辑在这里...
}

@Override
public void onCancelled(DatabaseError databaseError){

//逻辑失败在这里...
}
} );





$ p

$ b

解决方案

您的项目配置存在问题,导致Firebase身份验证令牌无法正常刷新。您需要在Firebase控制台中为您的应用添加SHA-1指纹。



转到Firebase控制台中项目的概述页面。选择相关应用的设置溢出菜单,然后点击管理。将会有一个按钮来添加SHA-1值。



您可以按照本指南。您可能需要重新下载并将google-services.json文件再次添加到Android Studio中的应用程序,但是我没有。



这个类似的堆栈溢出问题由Firebase团队成员回答,包含更多详细信息。


I've been using Firebase in my app successfully for several months. I just finished updating to use the 9.0.0 version, and I have one issue left that I can't figure out.

In my MainActivity onCreate function I check to see if the user is logged in to Firebase. If the user is logged in, then I attempt to fetch the user object that I have in my database.

This is the only instance in the app where i'm having trouble with this. I have verified that the user is signed in, and the DatabaseReference pointed to the right url.

The DatabaseReference is created, and the listener is added, but nothing on the listener ever fires...

    FirebaseAuth auth = FirebaseAuth.getInstance();

    if (auth != null && auth.getCurrentUser() != null) {
        DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child(USERS).child(auth.getCurrentUser().getUid());
        userRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Logic for success is here...
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                // Logic for failure is here...
            }
        });
    }

Any ideas about what i'm doing wrong?

解决方案

There is an issue with your project configuration that causes Firebase Auth tokens to not refresh properly. You need to add SHA-1 fingerprints for your apps in the Firebase console.

Go to the overview page of the project in the Firebase Console. Select the settings overflow menu of the app in question, and click "Manage". There will be a button to add SHA-1 values.

You can get SHA-1 values for your app by following the steps in this guide. You may need to re-download and add the google-services.json file to your app in Android Studio again, but I did not.

This similar stackoverflow question as answered by a Firebase team member contains more details if you require.

这篇关于Firebase单一值事件侦听器不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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