如何将Cloud Firestore中的异步数据另存为变量? [英] How to save asynchronous data from Cloud Firestore as a variable?

查看:87
本文介绍了如何将Cloud Firestore中的异步数据另存为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Android上的Firebase SDK学习 Cloud Firestore ,我我遇到了一个问题,我通过OnCompleteListener从Firestore数据库检索数据,然后尝试将其另存为变量.问题是,在OnCompleteListener结束并且我的Android应用程序移至下一行代码后,该变量中的数据将被重置而不保存.

I am beginning to learn Cloud Firestore using the Firebase SDK on Android, and I have run into an issue where I retrieve data from the Firestore database through an OnCompleteListener and then try to save it as a variable. The issue is that after the OnCompleteListener ends and my Android App moves on to the next line of code, the data in the variable is reset and not saved.

我有一个用户集合,正在尝试检索其季节内的一项运动:

I have a collection of users and am trying to retrieve a user's sport for the user's season:

db.collection("users").document(mAuth.getCurrentUser().getUid()).collection("seasons").document(seasonName)
                .get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document != null && document.exists() && document.getData() != null) {
                                sport = String.valueOf(document.getData().get("sport"));
                                //sport is set to the value
                            }
                        }
                    }
                });
        //Now sport is null

这项运动会设置为侦听器中的值,但在侦听器完成后会变为null.

The sport is set to the value in the listener, but then goes to null after the listener completes.

所以我的问题是,如何才能永久保存变量,以使它不会返回空值?有没有一种方法可以在没有侦听器的情况下检索数据,这样我就不会遇到这个问题,还是有其他解决方案?

So my question is, how can I save the variable permanently so that it doesn't go back to null? Is there a way to retrieve the data without a listener so I won't have this problem, or is there another solution?

提前谢谢!

推荐答案

变量不会返回"为null.问题是查询完成后,您添加的侦听器将被调用一些未知的时间.您不知道要花多长时间.由于时间未知,因此 get()将立即返回a Task,并且您附加到该Task的回调将在以后一段时间被调用.由于 get()立即返回,因此变量 sport 将保留其单独的null值,直到调用回调为止.这意味着您只应使用回调中的结果.或者,如果要将其存储在某个位置,请仅在确保已调用回调的情况下访问它.

The variable isn't "going back" to null. The issue is that the listener you added is being invoked some unknown amount of time after the query finishes. You don't know how long it's going to take. Because the time is unknown, the a Task is returned immediately by get(), and the callbacks you attach to it will be invoked some time later. Since get() returns immediately, the variable sport will retain its individual value of null until the callback is invoked. This means that you should only use the results from within the callback. Or, if you want to store it somewhere, only access it when you are sure the callback has been invoked.

实际上,所有Firebase API都是异步的.阅读以了解原因.您应该熟悉异步编程才能使用Firebase SDK.

In fact, all Firebase APIs are asynchronous. Read this to learn why. You should become familiar with asynchronous programming in order to use Firebase SDKs.

专门针对Android,您应该学习Play Services Task API的工作方式.

For Android specifically, you should learn how the Play Services Task API works.

这篇关于如何将Cloud Firestore中的异步数据另存为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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