java - Firestore异常: Backend ended Listen Stream

标签 java google-cloud-platform google-cloud-firestore

我正在尝试使用 Firestore 为集合设置实时监听器。每当在集合中添加、修改或删除文档时,我希望调用监听器。我的代码当前适用于一个集合,但是当我在更大的集合上尝试相同的代码时,它会失败并出现错误:

Listen failed: com.google.cloud.firestore.FirestoreException: Backend ended Listen stream: The datastore operation timed out, or the data was temporarily unavailable.

这是我实际的监听器代码:

/**
 * Sets up a listener at the given collection reference. When changes are made in this collection, it writes a flat
 * text file for import into backend.
 * @param collectionReference The Collection Reference that we want to listen to for changes.
 */
public static void listenToCollection(CollectionReference collectionReference) {

    AtomicBoolean initialUpdate = new AtomicBoolean(true);

    System.out.println("Initializing listener for: " + collectionReference.getId());

    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirestoreException e) {
            // Error Handling
            if (e != null) {
                System.err.println("Listen failed: " + e);
                return;
            }

            // If this is the first time this function is called, it's simply reading everything in the collection
            // We don't care about the initial value, only the updates, so we simply ignore the first call
            if (initialUpdate.get()) {
                initialUpdate.set(false);
                System.out.println("Initial update complete...\nListener active for " + collectionReference.getId() + "...");
                return;
            }

            // A document has changed, propagate this back to backend by writing text file.
            for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {

                String docId = dc.getDocument().getId();
                Map<String, Object> docData = dc.getDocument().getData();

                String folderPath = createFolderPath(collectionReference, docId, docData);

                switch (dc.getType()) {
                    case ADDED:
                        System.out.println("Document Created: " + docId);
                        writeMapToFile(docData, folderPath, "CREATE");
                        break;
                    case MODIFIED:
                        System.out.println("Document Updated: " + docId);
                        writeMapToFile(docData, folderPath, "UPDATE");
                        break;
                    case REMOVED:
                        System.out.println("Document Deleted: " + docId);
                        writeMapToFile(docData, folderPath, "DELETE");
                        break;
                    default:
                        break;
                }
            }

        }
    });
}

在我看来,集合太大,并且集合的初始下载超时。我可以使用某种解决方法来实时获取此集合的更新吗?

最佳答案

我联系了 Firebase 团队,他们目前正在就该问题回复我。与此同时,我能够通过基于上次更新时间戳属性查询集合来减小监听器的大小。我只查看最近更新的文档,并让我的应用程序在发生更改时更改此属性。

关于java - Firestore异常: Backend ended Listen Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725079/

相关文章:

java - Apache POI : How to insert column in Excel file

python - 如何使用基于 token 的访问获取虚拟机的 GCP 配额限制

google-cloud-firestore - onSnapshot : FirebaseError: [code=invalid-argument]: transaction closed 中的错误

java - 从座位预订系统中获取连续 3 个值?

Java - SHA-256 哈希 : Invalid AES key length : 64 bytes

java - 使用 Intellij IDEA 开发 Springboot 应用程序 - Ultimate Version 2018

google-cloud-platform - 如何使用 gcloud 命令行界面启用 Google Cloud Secrets Manager?

java - 在 pubsub 模拟器上创建主题

ios - Firebase 实时更新我关注的人的帖子

firebase - 如何使用本地缓存并仅使用 Firestore 更新已更改的文档?