0
votes

I am trying to access a field labeled vote_count via Cloud Firestore realtime updates.

Below is my data structure:

enter image description here

Previously, under Firebase Realtime Database, I would .addValueEventListener() and drill down to the "answer." However, with Cloud Firestore, it is a bit more complex.

    mStoreSelectedPollRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(final DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
            if (e != null){
                Log.v("LISTEN", "LISTEN_FAILED");
                return;
            }
            if (documentSnapshot != null){
                Log.v("Current Data", String.valueOf(documentSnapshot.getData()));
                mStoreSelectedPollRef.collection(ANSWERS_LABEL).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        updatePollResultAnswersDynamically(task.getResult().size(), documentSnapshot);
                    }
                });
            } else {
                Log.v("Current Data", "Current Data Nulll");
            }
        }
    });

Right now, I am adding a call to .get() within my Snapshot Listener, which seems to be inefficient. I was curious how I would access the:

  1. Total number of answer Documents.
  2. Each individual answer
1
I don't understand the code. It looks like it's saying "every time a certain document changes, query all the documents in the answers collection, count them, then do something with the first document". If you want to maintain a document count without having to pull an entire collection, there is a pattern for that. firebase.google.com/docs/firestore/solutions/countersDoug Stevenson
I essentially want to query the answers and get the size of the answers. Then i want to access the individual fields within each answer. In order to do this, it appears I need to have both .addSnapshotListener and .addOnCompleteListener(). Trying to figure out if there is a way to simplifytccpg288
OK, I'm confused. You have mStoreSelectedPollRef and you're essentially querying it twice, once for incremental changes, and again for size and content?Doug Stevenson
I guess I could add my .addSnapshotListener() to my answers node, but again I am not sure if the DocumentSnapshot it returns will give me the data I needtccpg288
I don't think you need a snapshot listener at all. Just query the entire collection once with get(), iterate the documents in it, and do what you want with each of them.Doug Stevenson

1 Answers

0
votes

Given this snippet:

mStoreSelectedPollRef.collection(ANSWERS_LABEL).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        updatePollResultAnswersDynamically(task.getResult().size(), documentSnapshot);
    }
});

You can get the total number of answer documents with:

task.getResult().size()

Since you already have this code, I'm not really sure what you're asking. If you're asking if there is a way to get the count without getting the documents, look here: https://stackoverflow.com/a/46555026

To access the individual answer documents, you loop over the query snapshot:

for (DocumentSnapshot document : task.getResult()) {
    Log.d(TAG, document.getId() + " => " + document.getData());
}