【发布时间】:2023-04-09 17:25:01
【问题描述】:
我正在使用 Java 管理 SDK 在 GCE 实例中运行的服务中侦听 Firestore 集合中文档的更改。我在查询快照侦听器中使用带有单个线程的Executor,以确保我收到的事件按照它们到达的顺序依次处理。一个有代表性的例子如下:
public class ListenerClass implements EventListener<QuerySnapshot> {
Map<String, Object> documentMap = new HashMap<String, Object>();
public void onEvent(QuerySnapshot querySnapshot, FirestoreException error) {
for (DocumentChange dc : querySnapshot.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
case MODIFIED:
documentMap.put(dc.getDocument().getId(), dc.getDocument().getData());
break;
case REMOVED:
documentMap.remove(dc.getDocument().getId());
break;
}
}
}
}
public class WatchService {
public static void main(String[] args) {
WatchService watchService = new WatchService();
watchService.watch();
}
public void watch() {
//Initialize Firebase and get firestore instance
Executor executor = Executors.newFixedThreadPool(1);
ListenerClass listenerClass = new ListenerClass();
firestore.get("collection_name").addSnapshotListener(executor, listenerClass);
//wait
}
}
我想知道 Firestore 是否有可能无序发送事件。例如,如果文档 A 在时间 t1 和 t2 更新。如果 t1 和 t2 彼此非常接近,我可以在时间 t1 的文档快照之前获得时间 t2 的文档快照吗? 到目前为止,我还没有在测试中观察到这一点。我也没有看到其他人提到过这种行为。我只是想知道这是否有可能,我应该在我的代码中处理它吗?
【问题讨论】:
标签: java firebase google-cloud-firestore