Java firestore快照的视图更改代码
我已经阅读了Firestore的文档: 但在使用相同的代码时,我无法解析DocumentChange、EventListener等符号。 类似地,我无法解析诸如getDocumentChanges、getDocument、addSnapshotListener之类的方法 我已经导入了'com.google.firebase:firebase admin:5.8.0' build.gradle文件Java firestore快照的视图更改代码,java,firebase,google-cloud-firestore,firebase-admin,Java,Firebase,Google Cloud Firestore,Firebase Admin,我已经阅读了Firestore的文档: 但在使用相同的代码时,我无法解析DocumentChange、EventListener等符号。 类似地,我无法解析诸如getDocumentChanges、getDocument、addSnapshotListener之类的方法 我已经导入了'com.google.firebase:firebase admin:5.8.0' build.gradle文件 group 'firestore' version '1.0-SNAPSHOT' apply plu
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管理员不足以使Firestore正常工作。要解决这个问题,只需添加以下代码行:
compile 'com.google.firebase:firebase-firestore:11.8.0'
就在firebase管理依赖项之后。同步您的项目并重试。请提供包含您的类和build.gradle文件的a。@AlexMamo,Graddle文件:-组“firestore”版本“1.0-SNAPSHOT”应用插件:“java”sourceCompatibility=1.8存储库{mavenCentral}依赖项{testCompile group:'junit',name:'junit',version:'4.12'}依赖项{compile'com.google.firebase:firebase admin:5.8.0'}代码是URL中“查看快照之间的更改”部分的代码:请通过添加mcv和build.gradle文件编辑您的问题。@AlexMamo,我已更新了代码,请检查并告知我缺少什么?我更新了buildgradle文件,但仍然无法解决导入问题。请尝试清理您的项目并重新编译再重复一遍。