【发布时间】:2020-03-22 22:29:32
【问题描述】:
我编写的以下方法运行良好,在我的 Utils 包中,我从我的一些活动中调用它。
private static Date date = null;
public static Date getCurrentTime(final Context context){
FirebaseFunctions firebaseFunctions;
firebaseFunctions = FirebaseFunctions.getInstance();
firebaseFunctions.getHttpsCallable("currentTime").call()
.addOnCompleteListener(new OnCompleteListener<HttpsCallableResult>() {
@Override
public void onComplete(@NonNull Task<HttpsCallableResult> task) {
try{
String dateString = task.getResult().getData().toString();
System.out.println("555555555555 TIME : " + dateString);
date = new Date(Long.getLong(dateString));
}
catch(Exception e){
convertFirebaseExceptionToAlertDialog(context, "A network error");
}
}
});
while (date == null){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return date;
}
我想知道是否有比通过循环检查值以从此类方法返回值更好的方法。 (获取值的方法会从onComplete、onSuccess等内部方法返回)
【问题讨论】:
标签: java android firebase asynchronous google-cloud-functions