Firestore – Unterschied zwischen DocumentSnapshot und QueryDocumentSnapshot

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Firestore – Unterschied zwischen DocumentSnapshot und QueryDocumentSnapshot

by Guest » 04 Jan 2025, 05:29

In der Dokumentation heißt es:

Ein QueryDocumentSnapshot enthält Daten, die im Rahmen einer Abfrage aus einem Dokument in Ihrer Firestore-Datenbank gelesen wurden. Die Existenz des Dokuments ist garantiert und seine Daten können mit den Methoden getData() oder get() extrahiert werden.
QueryDocumentSnapshot bietet die gleiche API-Oberfläche wie DocumentSnapshot. Da Abfrageergebnisse nur vorhandene Dokumente enthalten, gibt die Methode „exists()“ immer true zurück und getData() wird niemals null sein.

https://firebase .google.com/docs/reference/android/com/google/firebase/firestore/QueryDocumentSnapshot
Aber es erklärt nicht, wann ich eines über das andere verwenden sollte. Ich habe beides in einem SnapshotListener für eine Sammlung ausprobiert und beide haben funktioniert.

Code: Select all

protected void onStart() {
super.onStart();
notebookRef.addSnapshotListener(new EventListener() {
@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);
}
});
}

Top