【问题标题】:Code for view changes for firestore snapshotsfirestore 快照的视图更改代码
【发布时间】:2018-01-30 22:33:03
【问题描述】:

我已经浏览了 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);
    }

}

【问题讨论】:

  • 请提供mcv,包括您的班级和您的 build.gradle 文件。
  • @AlexMamo, Graddle File :- group 'firestore' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name :'junit',版本:'4.12' } 依赖项 { 编译 'com.google.firebase:firebase-admin:5.8.0' }
  • 代码位于 URL 中的“查看快照之间的更改”部分:firebase.google.com/docs/firestore/query-data/listen?authuser=0
  • 请编辑您的问题,添加 mcv 和 build.gradle 文件。
  • @AlexMamo,我已经更新了代码,请您查看并告诉我我缺少什么?

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


【解决方案1】:

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

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

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

【讨论】:

  • 我更新了 BuildGraddle 文件,但仍然无法解决导入问题。
  • 尝试清理您的项目并重新构建它。
猜你喜欢
  • 2019-08-13
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多