Android Firestore-DocumentSnapshot和QueryDocumentSnapshot之间的区别

Android Firestore-DocumentSnapshot和QueryDocumentSnapshot之间的区别,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,文件上说 QueryDocumentSnapshot包含作为查询的一部分从Firestore数据库中的文档读取的数据。文档保证存在,并且可以使用getData()或get()方法提取其数据 QueryDocumentSnapshot提供与DocumentSnapshot相同的API表面。由于查询结果只包含现有文档,exists()方法将始终返回true,getData()永远不会为null 但这并不能解释我什么时候应该用一个而不是另一个。我在快照监听器的集合中尝试了这两种方法,都成功了 pro

文件上说

QueryDocumentSnapshot包含作为查询的一部分从Firestore数据库中的文档读取的数据。文档保证存在,并且可以使用getData()或get()方法提取其数据

QueryDocumentSnapshot提供与DocumentSnapshot相同的API表面。由于查询结果只包含现有文档,exists()方法将始终返回true,getData()永远不会为null

但这并不能解释我什么时候应该用一个而不是另一个。我在
快照监听器
集合
中尝试了这两种方法,都成功了

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);
        }
    });
}
受保护的void onStart(){
super.onStart();
notebookRef.addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot queryDocumentSnapshots,FirebaseFirestoreException e){
如果(e!=null){
Toast.makeText(MainActivity.this,“加载时出错!”,Toast.LENGTH_SHORT.show();
Log.d(标记,例如toString());
返回;
}
字符串allNotes=“”;
用于(QueryDocumentSnapshot文档快照:QueryDocumentSnapshot){
Note=documentSnapshot.toObject(Note.class);
String title=note.getTitle();
字符串描述=note.getDescription();
所有注释+=“\n标题:“+标题+”说明:“+说明;
}
textViewData.setText(所有注释);
}
});
}
如您所述:

QueryDocumentSnapshot提供与DocumentSnapshot相同的API表面

这是因为是DocumentSnapshot的子类。这意味着可以将每个QueryDocumentSnapshot分配(向下转换)给DocumentSnapshot类型的变量。他们做的事情完全一样,除了你所说的他们之间的区别:

由于查询结果只包含现有文档,exists()方法将始终返回true,getData()永远不会为null

因此,如果您处理的是QueryDocumentSnapshot,那么可以保证exists()方法将返回什么。如果您处理的是DocumenSnapshot(实际上不是一个被降级的QueryDocumentSnapshot),那么您就没有这个保证


我想你可能太看重一个是另一个的子类这一事实了。只需使用API文档中显示的类型,不要将任何内容强制转换为其他类型,除非您确实知道需要这样做。

我不理解您的问题。您永远不会决定使用哪一个,API会告诉您使用哪一个。在您显示的代码中,侦听器接收到一个QuerySnapshot,它生成多个QueryDocumentSnapshot对象。DocumentSnapshot从未显示在此处-您不能同时使用它们。您有一个名为
documentSnapshot
的变量,但这不是同一回事。如果我在for循环中将
QueryDocumentSnaptshot
替换为
DocumentSnaptshot
,则应用程序的行为相同。但是,一个重要的区别是,QueryDocumentSnapshot通常不包含文档的引用路径,而DocumentSnapshot是。