当前位置: 首页>>代码示例>>Java>>正文


Java OnCompleteListener类代码示例

本文整理汇总了Java中com.google.firebase.tasks.OnCompleteListener的典型用法代码示例。如果您正苦于以下问题:Java OnCompleteListener类的具体用法?Java OnCompleteListener怎么用?Java OnCompleteListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


OnCompleteListener类属于com.google.firebase.tasks包,在下文中一共展示了OnCompleteListener类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: registrationStatus

import com.google.firebase.tasks.OnCompleteListener; //导入依赖的package包/类
@ApiMethod(path = "status", httpMethod = ApiMethod.HttpMethod.GET)
public RegistrationResult registrationStatus(ServletContext context, @Named("firebaseUserToken") String firebaseUserToken)
    throws IOException, ForbiddenException, ExecutionException, InterruptedException, InternalServerErrorException {

    String databaseUrl = context.getInitParameter("databaseUrl");
    LOG.info("databaseUrl: " + databaseUrl);
    String serviceAccountKey = context.getInitParameter("accountKey");
    LOG.info("accountKey: " + serviceAccountKey);
    InputStream serviceAccount = context.getResourceAsStream(serviceAccountKey);
    LOG.info("serviceAccount: " + serviceAccount);

    firebaseWrapper.initFirebase(databaseUrl, serviceAccount);
    firebaseWrapper.authenticateFirebaseUser(firebaseUserToken);

    if (!firebaseWrapper.isUserAuthenticated()) {
        throw new ForbiddenException("Not authenticated");
    }

    boolean isRegistered = isUserRegistered(context);

    final TaskCompletionSource<Boolean> isRegisteredTCS = new TaskCompletionSource<>();
    final Task<Boolean> isRegisteredTCSTask = isRegisteredTCS.getTask();
    // Update the user registration state in the Real-time Database.
    DatabaseReference dbRef = firebaseWrapper.getDatabaseReference();
    int rtdbRetries = 0;
    while (rtdbRetries < RTDB_RETRY_LIMIT) {
        dbRef.child("users").child(firebaseWrapper.getUserId()).setValue(isRegistered)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(Task<Void> task) {
                    if (task.isSuccessful()) {
                        isRegisteredTCS.setResult(true);
                    } else {
                        isRegisteredTCS.setResult(false);
                    }
                }
            });
        // If writing to RTDB was successful break out.
        if (Tasks.await(isRegisteredTCSTask)) {
            break;
        }
        LOG.info("Writing to RTDB has failed.");
        rtdbRetries++;
    }

    // If retry limit was reached return false, user will have to try again if they were
    // indeed registered.
    if (rtdbRetries >= RTDB_RETRY_LIMIT) {
        throw new InternalServerErrorException("Unable to write registration status to RTDB.");
    } else {
        // Return the user registration state.
        return new RegistrationResult(isRegistered);
    }
}
 
开发者ID:google,项目名称:iosched,代码行数:55,代码来源:RegistrationEndpoint.java


注:本文中的com.google.firebase.tasks.OnCompleteListener类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。