本文整理汇总了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);
}
}