java - Firestore 快照的 View 更改代码

标签 java firebase google-cloud-firestore firebase-admin

我已经浏览了Firestore的文档:https://firebase.google.com/docs/firestore/query-data/listen?authuser=0

但是在使用相同的代码时,我无法解析 DocumentChange、EventListener 等符号。 同样,我无法解析 getDocumentChanges、getDocument、addSnapshotListener 等方法。

我已经导入了“com.google.firebase:firebase-admin:5.8.0”。

build.gradle 文件

group 'firestore'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
dependencies {
  compile 'com.google.firebase:firebase-admin:5.8.0'
}

这是代码,我正在尝试:

package firestore;

import com.google.api.core.SettableApiFuture;
import com.google.firestore.v1beta1.DocumentChange;
import com.google.cloud.firestore.DocumentChange;
import com.google.cloud.firestore.DocumentChange.Type;
import com.google.cloud.firestore.EventListener;
import com.google.cloud.firestore.ListenerRegistration;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreException;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import static com.google.api.ChangeType.ADDED;
import static com.google.api.ChangeType.MODIFIED;
import static com.google.api.ChangeType.REMOVED;

/**
 * Snippets to demonstrate Firestore 'listen' operations.
 */
@SuppressWarnings("Convert2Lambda")
public class FirestoreChange {
    private static final long TIMEOUT_SECONDS = 5;

    private final Firestore db;

    FirestoreChange(Firestore db) {
        this.db = db;
    }

    /**
     * Listen to a query, returning the list of DocumentChange events in the first snapshot.
     */
    List<DocumentChange> listenForChanges() throws Exception {
        SettableApiFuture<List<DocumentChange>> future = SettableApiFuture.create();

        // [START listen_for_changes]
        db.collection("cities")
                .whereEqualTo("state", "CA")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot snapshots,
                                        @Nullable FirestoreException e) {
                        if (e != null) {
                            System.err.println("Listen failed: " + e);
                            return;
                        }

                        for (DocumentChange dc : snapshots.getDocumentChanges()) {
                            switch (dc.getType()) {
                                case ADDED:
                                    System.out.println("New city: " + dc.getDocument().getData());
                                    break;
                                case MODIFIED:
                                    System.out.println("Modified city: " + dc.getDocument().getData());
                                    break;
                                case REMOVED:
                                    System.out.println("Removed city: " + dc.getDocument().getData());
                                    break;
                                default:
                                    break;
                            }
                        }
                        // [START_EXCLUDE silent]
                        if (!future.isDone()) {
                            future.set(snapshots.getDocumentChanges());
                        }
                        // [END_EXCLUDE]
                    }
                });
        // [END listen_for_changes]

        return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }

}

最佳答案

发生这种情况是因为您根本没有在 build.gradle 文件中添加 Cloud Firestore 依赖项。添加 firebase-admin 不足以使 Firestore 正常工作。因此,要解决这个问题,只需添加这行代码:

compile 'com.google.firebase:firebase-firestore:11.8.0'

就在 firebase-admin 依赖项之后。同步您的项目并重试。

关于java - Firestore 快照的 View 更改代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48531612/

相关文章:

java - Java 十进制转二进制作业

javascript - Firebase 功能警告 - 一致的返回

javascript - 输出嵌套集合中的所有文档

Java Flight Control - 保留最后 N 分钟记录数据

java - GWT doGet() servlet 将图像字节数组或图像返回给客户端

java - 必须为 2 名玩家编写 Tic Tac Toe 游戏

firebase - 共享 Firestore 文档或将协作者添加到 Firestore 文档

android - Firebase Analytics 调试 View 不显示任何内容

php - Firebase 云消息传递 PHP 集成发送推送通知

firebase - 从 firebase 函数触发器访问 firebase 托管文件