1

I have the following code,

public CardsController(){
    final Map<Integer, Card> temp = new HashMap<>();
    for (int i=0; i<CardsInAlbum; i++){
        temp.put(i, new Card(i));
    }

    String uID = FirebaseAuth.getInstance().getUid();
 [1]FirebaseFirestore.getInstance()
            .collection("albums").document(uID)
            .collection("cards").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            Card c = getCard(document.getData());
                            Log.d("CardsController", c.getNumber() + "" + " number:" + c.getNumber() + " state:" + c.getState());
 [2]                        temp.put(c.getNumber(), c);
                        }
                    } else {
 [3]                        Log.d(Tag, "Error getting documents: ", task.getException());
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
 [4]        Log.d(Tag, e.getMessage());
        }
    });

 [5]Cards = temp;
}

I have set breakpoints in the lines [1] to [5], when debugging it first stops in [1] and then debugging line by line seems like it also reach the addOnCompleteListener and addOnFailureListener but it skips [2], [3] and [4]. I remember it was working (with no changes made to this code), so I don't know if there is some kind of cache from firebase as the offline database that prevents getting information from database, but I tried reseting the emulator and still does not work, any idea why could be happening with this?

PD: Checked logcat, there is none of the logged messages in the function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fabian Merchan
  • 943
  • 8
  • 15
  • The asynchronous nature of the Firebase API is [explained here](https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93) – Bob Snyder Feb 17 '18 at 06:21
  • You can also take a look [here](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). – Alex Mamo Feb 17 '18 at 12:33
  • Bob and Alex Thank you guys! – Fabian Merchan Feb 17 '18 at 17:50
  • Discovering there was an addOnFailureListener event was very helpful, and by using it enabled me to discover my problem. – Alyoshak May 03 '22 at 20:56

2 Answers2

0

Use either only OnCompleteListener or use OnFailureListener and OnSuccessListener combinations.

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31
0

Thank you Bob and Alex for helping me to understand the firebase asynchronous behavior.

The behaviour I was describing, happens because of the asynchronous nature of Firebase API, it means that it don't block the main thread while making the request to the server, instead it make the request in a separate thread and returns it when ready.

My mistake was to show all the values in the activity without waiting for the database to respond. I had to place a loader animation while Firebase returns me the value.

Fabian Merchan
  • 943
  • 8
  • 15