我正在使用firestore实时更新,我有以下侦听器
CollectionReference collecRef = db.collection("lists").document("doc1").collection("locations");
changesListener=collecRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "listen:error", e);
return;
}
a
for (DocumentChange dc : snapshots.getDocumentChanges())
{
DocumentSnapshot document = dc.getDocument();
// this is where I expected to loop over all documents. But instead it is only one item..the changed the document
}
}
});
根据文件:
查看查询结果在
查询快照,而不是简单地使用整个查询快照。
这意味着,当修改文档时,我希望整个查询重新运行,因为结果发生了更改。但是,在监听器中,我只获取已更改文档的文档快照,而不是所有查询结果(即在我的示例中是所有文档)
我是不是丢了什么东西?
最佳答案
如果你打电话给snapshots.getDocumentChanges()
,你只会得到已更改的文档。如果要获取所有文档,请致电QuerySnapshot.getDocuments()
。
for (Document document : snapshots.getDocuments()) {
... do something with document ...
关于android - 实时更新仅返回更改的文档,而不返回整个查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49243922/