-1

Right now I have working code to grab a DocumentReference from a Firestore database by id. I'm trying to then perform a .get() command to process a particular field from the Document and nothing is happening. Here's my code:

FirebaseFirestore mFirestore = FirebaseFirestore.getInstance();
DocumentReference docRef = mFirestore.collection(collection).document(doc_key);
Log.d("LOGGER", "docRefID is:" + docRef.getId());
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        Log.d("LOGGER", "task complete");   // this is never being called
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                Log.d("LOGGER", "Got in here");
                foo(document.getString(myField));  // this is where I'm trying to do things, but its not getting here
            } else {
                Log.d("LOGGER", "No such document");
            }
        } else {
            Log.d("LOGGER", "get failed with ", task.getException());
        }
    }
});
while( // foo has not been called yet ) {
    Log.d("LOGGER", "still waiting");
    Thread.sleep(2000);
}

Here's my logcat output:

2020-04-13 13:55:47.449 24669-24669/com.example.myurbanflix D/LOGGER: docRefID is:nNuztOjrN0kz53XTmCS1
2020-04-13 13:55:47.450 24669-24669/com.example.myurbanflix D/LOGGER: still waiting
2020-04-13 13:55:49.452 24669-24669/com.example.myurbanflix D/LOGGER: still waiting
2020-04-13 13:55:51.452 24669-24669/com.example.myurbanflix D/LOGGER: still waiting

When I run the program, I am properly seeing my DocumentReference's ID and it IS the value of the ID of the document I'm trying to query, but NONE of the other Log messages are appearing. It seems like the task is never completing because none of those inner Log messages are appearing and my while loop at the bottom just keeps pinging logcat every two seconds. I should also note for context I am trying to do this inside a FirestoreAdapter for use in a RecyclerView. Any idea what I'm doing wrong?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This won't work: `while() { Log.d("LOGGER", "still waiting"); Thread.sleep(2000); }` Instead, have a look at this answer to see how to property respond once the data has loaded: https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413 – Frank van Puffelen Apr 14 '20 at 02:27

1 Answers1

0

Welcome! Nice to have you on board!

I couldn't find any reference for this problem, however looking at general concept of asynchronous programming, I think this infinite while loop is blocking main tread of your code. As the loop is whole synchronous it will block main tread. Asynchronous code is not able to return results to main tread.

You may try to look for asynchronous methods for delay (I tried to Google it and there are many) However I suggest to remove the loop as this should work fast enough.

I hope it will help!

vitooh
  • 4,132
  • 1
  • 5
  • 16