2017-11-21 1 views
1

Javaベースの新しいfirebase-admin SDKの廃止予定ノートを解決しようとしていますが、コードは5.3.1以降で書かれています登場廃止ノート5.5.0にバージョンをアップグレードし、ここに私のコードのサンプル:FirebaseDatabasefirebase admin SDK 5.4以降のdepotated TaskからApiFutureへの移行方法

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; 
    } 

そしてのために:

(:TaskaddOnSuccessListeneraddOnFailureListenerにdeprecatation)FirebaseAuthを使用して(:TaskaddOnSuccessListeneraddOnFailureListenerupdateChildrenremoveValue上deprecatation):

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を使用する必要が廃止ノートsayign: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>}. 
*/ 
+0

新しいAPIを調べてみましたか? –

+0

@DougStevenson https://firebase.google.com/docs/database/adminまたはhttps://firebase.google.com/docs/database/androidを意味する場合、ドキュメントは古いバージョンのままで、まだ更新されていないようですあなたは 'addListener(Runnable listener、Executor executor)'を意味しています。これは 'Runnable listener'か' toCompletableFuture'の部分をどうやって行うのかを聞いています。 –

答えて

1

ApiFutureへのコールバックを追加することができますApiFutures utilのクラス、見てみましょう。

+0

興味深いことに、これをチェックします –

+0

Nice answer、男!ありがとう! –

0

ApiFutureを使用してFirebase管理SDK、JavaからFirebaseAuthでトークンを検証するコードは、次のようになります。

ApiFutures.addCallback(FirebaseAuth.getInstance().verifyIdTokenAsync(token), 
    new ApiFutureCallback<FirebaseToken>() { 
     @Override 
     public void onFailure(Throwable t) { 
     // TODO handle failure 
     } 
     @Override 
     public void onSuccess(FirebaseToken decodedToken) { 
     // TODO handle success 
     } 
}); 

同様のアプローチがFirebaseDatabaseを使用して、あなたのコードのために使用することができます。

Hiranya JayathilakaがApiFutureへのタスクから移行する方法について説明非常に詳細な記事を書いて、その背後にある合理的:https://medium.com/google-cloud/firebase-asynchronous-operations-with-admin-java-sdk-82ca9b4f6022

このコードは、バージョン5.4.0のために働く - の時点で5.8.0、最新のこの執筆。 リリースノートは次のURLから入手できます:https://firebase.google.com/support/release-notes/admin/java

関連する問題