The documentation says
A QueryDocumentSnapshot contains data read from a document in your Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted using the getData() or get() methods.
QueryDocumentSnapshot offers the same API surface as DocumentSnapshot. Since query results contain only existing documents, the exists() method will always return true and getData() will never be null.
But it doesn't explain when I should use one over the other. I tried both in a SnapshotListener on a Collection and both worked.
protected void onStart() {
super.onStart();
notebookRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Toast.makeText(MainActivity.this, "Error while loading!", Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
return;
}
String allNotes = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Note note = documentSnapshot.toObject(Note.class);
String title = note.getTitle();
String description = note.getDescription();
allNotes += "\nTitle: " + title + " Description: " + description;
}
textViewData.setText(allNotes);
}
});
}
documentSnapshot, but that's not the same thing. - Doug StevensonQueryDocumentSnaptshotforDocumentSnaptshotin the for loop, the app behaves the same. - Florian Walther