A
A
AndreyRafalsky132019-02-26 08:52:02
Android
AndreyRafalsky13, 2019-02-26 08:52:02

Strange behavior of method for getting data from Firebase collection. How to fix?

Why is the method for getting data from the collection misbehaving?

public List<Note> loadNotes(){
    database.collection(NoteCollection.COLLECTION_NAME)
            .get()
            .addOnSuccessListener(queryDocumentSnapshots -> {
                notes.addAll(queryDocumentSnapshots.toObjects(Note.class));
                Log.d("loadingNote", String.valueOf(notes.size()));
            })
            .addOnFailureListener(e -> {
                Log.d("loadingNote", e.toString());
            });
    Log.d("loadingNote", String.valueOf(notes.size()));
    return notes;
}

The matter is that addOnSuccessListener fulfills correctly. That is, two elements are added to the sheet. But before return I check the sheet and it turns out that there are no elements in it at all. Although they were added to addOnSuccessListener. Why is this happening and how to solve this problem?
Here are the logs:
2019-02-26 07:42:04.194 12548-12548/com.rafalsky.firebaselesson1 D/loadingNote:
0

Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-02-26
@AndreyRafalsky13

Well, even the logs show everything.
This is an asynchronous operation, the addOnSuccessListener callback is called after your method has exited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question