I am trying to access a field labeled vote_count
via Cloud Firestore realtime updates.
Below is my data structure:
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:
- Total number of answer Documents.
- Each individual answer