Asynchronous Firebase API - Cloud Firestore and Android

Alex Mamo · 8K views · 2018 · 7:22

Open on YouTube

Software practice, open source & careersWeb developmentTesting, debugging & developer toolstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when someone's Firestore list or variable comes back empty or null because the data hasn't loaded yet, usually alongside a custom callback fix.

Recommended by 3 people across 16 different months, 2018 to 2022.

What it is

A 7-minute Android tutorial by Alex Mamo explaining why Cloud Firestore calls are asynchronous and how to handle that with a custom callback interface. It shows how to get data out of onComplete() or onSuccess() listeners instead of reading empty variables.

What people said

Recommends Alex Mamo · 2018-03 · Stack Overflow · on “Spinner NOT Working Well with Firestore's document.getId”

This is happening because you are not defining your spinnerArray inside the onComplete() method, which has an asynchronous behaviour. To solve this, move the declaration of your spinnerArray inside the onComplete() method. To display your records you also need to set the adapter inside the method. For more information please see my answer from this post [LINK] . I also recommend that you see this video, Asynchronous Firebase API - Cloud Firestore and Android [this video] , for a better understanding.

Recommends Alex Mamo · 2018-03 · Stack Overflow · on “Android Firestore Get Collections From Multiple Locations”

To solve this, move the declaration of your productModelList : final List<ProductModel> productModelList = new ArrayList<>(); inside the onComplete() method, right before the starting of your for loop. The list should also be inside that method, otherwise it will be empty, due the asynchronous behaviour of the method which is called even before you are trying to add those objects to the list. For a better understanding, I recommend you see my answer from this post [LINK] and also this video [this video] .

Recommends Alex Mamo · 2018-04 · Stack Overflow · on “Get the reference of the document before adding the data to collection in Firestore”

return this reference, How? You cannot return something now that hasn't been loaded yet. With other words, you cannot simply use that result outside the onSuccess() method because it will always be null due the asynchronous behaviour of this method. This means that by the time you are trying to use that result outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use those results only inside the onSuccess() method, otherwise I recommend you see the last part of my anwser from this post [LINK] in which I have explained how it can be done using a custom callback. You can also take a look at this

Recommends Alex Mamo · 2018-05 · Stack Overflow · on “Firebase Auth returns wrong value”

You cannot return something now that hasn't been loaded yet. With other words, you cannot simply use the userAuthStatus object outside the onComplete() method because it will always have the default value of Status.SIGNED_OUT due the asynchronous behaviour of this method. This means that by the time you are trying to use that result outside that method Log.w(TAG, "returns" + userAuthStatus); , the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use that result only inside the onComplete() method, or if you want to use it outside that method, I recommend you see the last part of my anwser from this post [LINK] in

Recommends Alex Mamo · 2018-05 · Stack Overflow · on “I get "null" when I try to access a variable from inner class in firebase methods”

You cannot use something now that hasn't been loaded yet. With other words, you cannot simply use the username outside the onDataChange() method because it will always be null due the asynchronous behaviour of this method. This means that by the time you are trying to print that result outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use that results only inside the onDataChange() method, otherwise I recommend you see the last part of my anwser from this post [LINK] in which I have explained how it can be done using a custom callback. You can also take a look at this video [this video] for a be

Recommends Alex Mamo · 2018-05 · Stack Overflow · on “Read Data from Cloud FireStore Android”

You cannot use something now that hasn't been loaded yet. With other words, you cannot simply use the following line of code: id_customer = customers.get(0).getId_customer(); Outside the onSuccess() method because it will always be null due the asynchronous behaviour of this method. This means that by the time you are trying to use the id_customer variable outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use that result only inside the onSuccess() method, or if you want to use it outside, I recommend you see the last part of my anwser from this post [LINK] in which I have exaplined how it

Recommends Alex Mamo · 2018-06 · Stack Overflow · on “I need a return getting a value from inside the callback method”

I recommend you see the last part of my anwser from this ** post [LINK] ** and also take a look at this ** video [this video] **.

Recommends Alex Mamo · 2018-08 · Stack Overflow · on “Android studio recylerview in fragment using data from firestore”

having debugged it I've found that this initially returns an empty list. Yes, this the normal behaviour. You cannot use now something that hasn't been loaded yet. With other words, you cannot simply create the leaguesList variable as global variable and use it outside the onEvent() method because it will always be empty due the asynchronous behaviour of this method. This means that by the time you are trying to use that list outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use leaguesList list only inside the onEvent() method, otherwise I recommend you see the last part of my anwser from t

Recommends Alex Mamo · 2018-08 · Stack Overflow · on “Querying a list of objects from Firebase Firestore on Android”

You cannot use now something that hasn't been loaded yet. With other words, you cannot simply make your projects ArrayList as a global variable and use its value outside the onComplete() method because it will always be null due the asynchronous behaviour of this method. This means that by the time you are trying to use the following log statement: Log.d(TAG, "End of method: " + projects.toString()); The data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to move this line of code inside the onComplete() method, otherwise I recommend you see the last part of my anwser from this post [LINK] in which I have explained how

Recommends Alex Mamo · 2018-12 · Stack Overflow · on “Querying data from Firestore to Arraylist but got nothing?”

You cannot simply use the posList list outside the onSuccess() method because it will always be empty due the asynchronous behaviour of this method. This means that by the time you are trying to print posList.toString() outside that method, the data hasn't finished loading yet from the database and that's why is not accessible. A quick solve for this problem would be to use the list only inside the onSuccess() method, as in the following lines of code: stores.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { List<GeoPoint> list = new ArrayList<>(); for (QueryDocumentSnapshot d