I have a little question here.
private boolean isSomethingTrue(String param) {
boolean result = false;
myService.hasAlerts(param,new Callback<Boolean>(
@Override
public void onSuccess(Boolean hasAlerts) {
result = hasAlerts;
}
});
return result;
}
On this code, how can i return the boolean hasAlerts that is received in the callback?
This doesn’t work because the result variable is not final.
But when it’s final, it can’t be modified so…
I’ve done something like that:
private boolean isSomethingTrue(String param) {
class ResultHolder {
boolean result=false;
}
final ResultHolder resultHolder = new ResultHolder();
myService.findBoolean(param,new Callback<Boolean>(
@Override
public void onSuccess(Boolean hasAlerts) {
resultHolder.result = hasAlerts;
}
});
return resultHolder.result;
}
But is there a simpler solution to handle such a case?
I’ve found this problem while trying to call a GWT RPC service.
I can think of a few variations–none of them particularly exciting. You could merge the result holder and callback into a single class and make it static if you could use it elsewhere, but it’s not really an improvement.
You could implement a generic synchronous
Future, but that might be misleading.Finally, if you’re doing this often you could genericize the value holder.