android - Firebase 变量未更新

标签 android firebase firebase-realtime-database

奇怪的问题让我有点不解。

我正在尝试访问 Firebase 数据库中的值。

final long[] time = {0};
    firebaseAuth = FirebaseAuth.getInstance();
        email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
        DocumentReference docRef = db.collection("location_times").document(email);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
                        time[0] = (long) document.get("time");
                        Log.e(TAG, "Location frequency is " + time[0] + " seconds.");

                    } else {
                        Log.e(TAG, "No such document");
                    }
                } else {
                    Log.e(TAG, "get failed with ", task.getException());
                }
            }
        });

    Log.e(TAG, "Time is: " + time[0]);

time 变量是我想要访问的。

Logcat 显示以下内容:

Location frequency is 5 seconds.
Time is: 0

问题是什么?似乎可以正常访问数据库,但变量未更新。我做错了什么?

提前致谢!

最佳答案

它是 0 因为它还没有更新,因为 firebase 运行一个异步任务来完成监听器。

您正在访问 Log.e(TAG, "Time is: "+ time[0]); onComplete 监听器外的代码行。这行代码甚至在 firebase 完成其获取之前就已执行。您可以实现方法调用并从那里访问时间。

final long[] time = {0};
firebaseAuth = FirebaseAuth.getInstance();
    email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
    DocumentReference docRef = db.collection("location_times").document(email);
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
                    time[0] = (long) document.get("time");
                    Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
                    showTime(); // Added a method call
                } else {
                    Log.e(TAG, "No such document");
                }
            } else {
                Log.e(TAG, "get failed with ", task.getException());
            }
        }
    });

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}

关于android - Firebase 变量未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240001/

相关文章:

java - 参数 'pathString' 为空

firebase - 使用 Netlify Lambda 函数通过 Auth0 对 Firebase 进行身份验证

java - Android:使用两个 ValueEventListener 从 firebase 数据库获取数据(在 Arraylist 过期之前设置适配器)

javascript - 从天气网站抓取完整的 html 数据

Swift 5,如何在从 Firebase 获取所有添加的子项后执行代码

android - 每隔几秒接收一次 TIMEZONE_CHANGED Intent

java - 使用邮箱和密码注册成功后,我可以将邮箱自动添加到实时数据库吗?

java - 火力 : get data details from another child

android - 如何使用android蓝牙接收串行数据

java - Android - 当模型包含 byte[] 时,Retrofit 被接收为 null