0

Preliminary Note: I am running a method called getStatesList(), which returns an ArrayList. I am using this in tandem with a Runnable, which collects the data a few seconds later and updates the view. Basically, I run the call during onResume() so the user will see the activity, showing that it is loading data. If I retrieve the data successfully, that is great. If not, the user can retry (and check internet connection). About 99 percent of the time, I am able to retrieve the queried data.

My Question: I am having an issue collecting ALL documents within my root collection, "DATA". I run the below code (in tandem with a Runnable), but only "CA" shows up in the results. I believe this is because both "AL" and "MI" are no longer documents, but references to other collections, which I have named sub-1 and sub-2 for this example. I have added an image to aid in my explanation. Why can I retrieve "CA" but not the other documents?

Also, is there a term for documents that point to collections? Knowing this may help me find a solution.

public ArrayList<String> getStatesList(){
    db.collection("DATA").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()) {
                        // In this example, I get one result returned ("CA")
                        for(QueryDocumentSnapshot document : task.getResult()){
                            states.add(document.getId());
                        }
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    states = null;
                }
            });

    return states;
}

enter image description here

**UPDATE 2/3/2020: I found that if I add a field to "AL", it will show up in the query. However, I'd still rather not add fields where they are not necessary. I'll do this for now, but I'd prefer a solution that doesn't require needless data. I'd also like to point out that "CA" has always shown up in the query without a single field of data. Any explanation for this?

FoxDonut
  • 252
  • 2
  • 14
  • `get()` is asynchronous and returns immediately. That means your function is going to return a list that is initially empty, until the query actually completes. Your callbacks will be invoke some time later. You will have to deal with Firestore asynchonrously instead of trying to make it return values immediately. – Doug Stevenson Feb 03 '20 at 20:07
  • @DougStevenson Hi Doug, thank you for your reply. However, I am able to read the documents contained as long they have a field, or the document is empty but does not point to a collection. The problem is that I do not need a field in my documents pointing to a collection. Currently, I allow 3 seconds for the transaction to complete. – FoxDonut Feb 03 '20 at 20:30
  • There is no way you can return your `states` list as a result of a method. Please check the duplicate to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Feb 04 '20 at 08:26
  • @AlexMamo Hi Alex, I deleted my last 2 comments here, but had to cool down a bit. I feel that i'm a good programmer and got slumped in with individuals who don't understand that firestore doesn't respond instantly (say, via requesting data instantly via a method). Please take a look at the "preliminary note" I added to my post. Maybe we can handle this via PM? Thanks buddy. – FoxDonut Feb 05 '20 at 06:59

0 Answers0