【发布时间】:2023-04-01 11:49:02
【问题描述】:
我只是想从我的新 firebase-admin SDK 的 Java 代码中解决弃用说明,该代码是在 5.3.1 版本中编写的,但是在将版本升级到 5.5.0 后出现了弃用说明,这里是我的代码示例:
使用 FirebaseAuth(已弃用:Task、addOnSuccessListener 和 addOnFailureListener):
private CompletableFuture<FirebaseToken> getDecryptedTokenCompletableFuture(String firebaseTokenString) {
CompletableFuture<FirebaseToken> tokenFuture = new CompletableFuture<>();
Task<FirebaseToken> tokenTask = FirebaseAuth.getInstance(firebaseApp).verifyIdToken(firebaseTokenString);
tokenTask.addOnSuccessListener(tokenFuture::complete);
tokenTask.addOnFailureListener(exception -> tokenFuture.completeExceptionally(new AuthorizationException("Failed to verify token", exception)));
return tokenFuture;
}
对于 FirebaseDatabase(已弃用:Task、addOnSuccessListener、addOnFailureListener、updateChildren 和 removeValue):
public static <T> CompletableFuture<T> toCompletableFuture(Task<T> task) {
CompletableFuture<T> future = new CompletableFuture<>();
task.addOnCompleteListener(result -> {
future.complete(result.getResult());
}).addOnFailureListener(future::completeExceptionally);
return future;
}
/**
* @param updatedParams if null it will removed child
* @param path path to update
* @return void when complete
*/
public CompletableFuture<Void> updateObjectData(Map<String, Object> updatedParams, String path) {
if (updatedParams == null) {
return removeObjectData(path);
}
logger.debug("Update ObjectData in firebase of ref ({}) with data: {}", path, updatedParams.toString());
DatabaseReference child = this.getUserDataReference().child(path);
return toCompletableFuture(child.updateChildren(updatedParams));
}
/**
* @param path path to of node to remove
* @return void when complete
*/
public CompletableFuture<Void> removeObjectData(String path) {
logger.debug("Remove ObjectData in firebase of ref ({})", path);
DatabaseReference child = this.getUserDataReference().child(path);
return toCompletableFuture(child.removeValue());
}
弃用说明说我必须使用 ApiFuture 作为发行说明所说的:https://firebase.google.com/support/release-notes/admin/java
还有内源,比如:
/** * Similar to {@link #updateChildrenAsync(Map)} but returns a Task. * * @param update The paths to update and their new values * @return The {@link Task} for this operation. * @deprecated Use {@link #updateChildrenAsync(Map)} */
和
/** * Represents an asynchronous operation. * * @param <T> the type of the result of the operation * @deprecated {@code Task} has been deprecated in favor of * <a href="https://googleapis.github.io/api-common-java/1.1.0/apidocs/com/google/api/core/ApiFuture.html">{@code ApiFuture}</a>. * For every method x() that returns a {@code Task<T>}, you should be able to find a * corresponding xAsync() method that returns an {@code ApiFuture<T>}. */
【问题讨论】:
-
您是否尝试过研究新 API 以了解其工作原理?
-
@DougStevenson 如果您的意思是 firebase.google.com/docs/database/admin 或 firebase.google.com/docs/database/android 如果您的意思是
addListener(Runnable listener, Executor executor),该文档似乎仍然适用于旧版本并且尚未更新,这就是我要问的 @987654338 @ 或者说我该怎么做toCompletableFuture部分
标签: java firebase java-8 firebase-admin