-3

I have a very confusing and frustrating issue. The method used here always returns null or 0

public int commentsNO(String tweeiID) {
    db2 = FirebaseFirestore.getInstance();
    //FireStore Comments reading
    db2.collection("Comments").
            whereEqualTo("TweetId", tweeiID)
            .get()
            .addOnCompleteListener(task -> {
     
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        counter++;
                    }
                    Log.d("ِLog1", "Counter Value inside Scope: " + counter);
                }
            });

    Log.d("Log2", "Counter Value outside Scope: " + counter);
    return counter;
}

The output is:

D/Log: Log2 Counter Value outside Scope: 0

D/ِLog: Log1 Counter Value inside Scope: 1

The counter value is set to 1 inside for loop scope , but when the loop is finished , the counter value seems to be set to 0!! It looks like as if the execution starts from the end, since Log1 is supposed to show first.

Afeef
  • 71
  • 6
  • 1
    Log2 showing first is expected. This is an async action. – Mark Rotteveel Dec 06 '22 at 11:21
  • 1
    `addOnCompleteListener` means "add a thing to happen later". You can't return a variable *now* and require it to have the value it is updated to later. – khelwood Dec 06 '22 at 11:23
  • Thank you for your reply...so how to get counter value from "addOnCompleteListener " and return it? – Afeef Dec 06 '22 at 11:37
  • 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 [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Dec 06 '22 at 12:11

1 Answers1

-2
public int commentsNO(String tweetID) {
  FirebaseFirestore db = FirebaseFirestore.getInstance();
  
  // Initialize the counter variable to 0.
  int counter = 0;

  // FireStore Comments reading
  db.collection("Comments")
    .whereEqualTo("TweetId", tweetID)
    .get()
    .addOnCompleteListener(task -> {
      if (task.isSuccessful()) {
        for (QueryDocumentSnapshot document : task.getResult()) {
          counter++;
        }

        Log.d("Log", "Counter Value inside Scope: " + counter);

        // Return the value of counter after the query has completed.
        return counter;
      }
    });

  // This line will not be executed because the method will return
  // before the Firestore query has completed.
  Log.d("Log", "Counter Value outside Scope: " + counter);
}
Taron Qalashyan
  • 660
  • 4
  • 8
  • 1
    This will not compile as there is no return within the method `commentsNO`. The return you added inside the complete listener does not return from commentsNO, and will likely not compile either, unless `addOnCompleteListener` is an `ToIntFunction<..>` or similar. – Mark Rotteveel Dec 06 '22 at 11:24