gpt4 book ai didi

java - Firestore 快照的 View 更改代码

转载 作者:行者123 更新时间:2023-12-02 11:41:14 24 4
gpt4 key购买 nike

我已经浏览了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com