0

I have data that should be visible in code that I get from FireStore collection and add it to ArrayList, I know that it happens because get() method returns immediately so the query isn't done yet, and hence the onCreate() method won't see any data, but is there a way to save that data in ArrayList and use it somewhere else?

For example here is two versions of code: 1st one make Toast with whole list like it should:

        db.collection("Users Data")
                .document(currentUserID)
                .collection("Follows")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                following.add(document.getId());
                                Toast.makeText(SearchedProfile.this, following.toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });

and 2nd make Toast with empty ArrayList (Toast says '[]')

        db.collection("Users Data")
                .document(currentUserID)
                .collection("Follows")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                following.add(document.getId());
                            }
                        }
                    }
                });

        Toast.makeText(SearchedProfile.this, following.toString(), Toast.LENGTH_SHORT).show();

Only difference is a position of Toast method.

  • because your code isn't synchronous - https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value. so make use of your data when you have it, or use callbacks – a_local_nobody Nov 24 '21 at 15:23
  • The `Toast` gets called before your `oncomplete` runs. As **a_local_nobody** pointed out you'd have to call your methods that use the data in a callback when `oncomplete` runs. – Alias Cartellano Nov 24 '21 at 16:39
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Nov 25 '21 at 07:36

1 Answers1

0

You received an empty array list because your code isn't synchronous.

In other words the Toast gets called before your oncomplete runs. You need to call your methods that use the data in a callback when oncomplete runs.

Antonio Ramirez
  • 943
  • 6
  • 15