The following method I wrote, which works fine, is in my Utils package and I call it from some of my activities.
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;
}
I want to know if there are better ways than checking the value by a loop to return a value from methods like this. (Methods that get the value will be returned from inner methods like onComplete, onSuccess)