将侦听器添加到 ApiFuture<WriteResult> 和 ApiFuture 时要传递哪些可运行内容<DocumentSnapshot>?



我正在使用Java语言的谷歌云函数将文档保存到谷歌云Firestore。

DocumentReference userDoc = firestore.document("Users/user1");
final Map<String,Object> UserData = new HashMap<String,Object>(){{
put("Name", "First Last");
}};
ApiFuture<WriteResult> future = userDoc.set(UserData);

我想在这里为future添加一个侦听器。

future.addListener(/* which Runnable listener to pass here to 
process the WriteResult object 
when the future completes? */, MoreExecutors.directExecutor());

我在这里找不到能达到这个目的的跑步者。我知道我可以调用future.get()方法来保持执行并获得WriteResult对象,但我不想这样做。

此外,无法理解如何将侦听器添加到userDoc.get()

我已经搜索了StackOverflow和Github。找不到任何有用的东西

所有的帖子都谈到了ApiFuture.addListener(),它已被弃用,现在不可用。

请在我的pom.xml 下面找到

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>some.group.Id</groupId>
<artifactId>cloud-functions</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.cloud.functions/functions-framework-api -->
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-auth -->
<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-auth -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-pubsub -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.114.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.10.0</version>
<configuration>
<functionTarget>com.infonotics.stocklyticsusa.FirstFunction</functionTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>

已经浪费了2天。请帮忙

根据文档,您可以使用ApiFutures.addCallBack:

static <V> void     addCallback(ApiFuture<V> future, ApiFutureCallback<? super V> callback, Executor executor) 

https://googleapis.github.io/api-common-java/1.9.1/apidocs/

示例:

ApiFuture<WriteResult> result1 = 
ApiFutures.addCallback(result1, new ApiFutureCallback<WriteResult>() {
@Override
public void onFailure(Throwable throwable) {
}
@Override
public void onSuccess(WriteResult writeResult) {
}
}, Runnable::run);

相关内容

  • 没有找到相关文章