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.