Reputation: 25
I have written the following code. This piece of code just basically retrieving the value from the database. But the issue is happening that it is returning the value before completion of the onSuccess method.
How to mitigate the problem?
private static final SomeServiceAsync service = GWT.create(SomeService.class);
private static boolean valueIndicator = true;
private static boolean checkValueOfVariable() {
service.isValueTurnedOn(new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) {
Window.alert("Failed to retrieve value."+caught.getLocalizedMessage());
}
@Override
public void onSuccess(Boolean value) {
valueIndicator = value;
}
});
return valueIndicator;
}
I want to return the value after execution of onSuccess method.
Upvotes: 0
Views: 104
Reputation: 3832
This will not work.
The server call is asynchron. So the method will return the value before the server call has returned.
Use a callback:
public interface ReturnHandler {
void onReturn(Boolean value);
}
Try:
private static void checkValueOfVariable(ReturnHandler handler) {
service.isValueTurnedOn(new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) {
Window.alert("Failed to retrieve value."+caught.getLocalizedMessage());
}
@Override
public void onSuccess(Boolean value) {
handler.onReturn(value);
}
});
}
and call it using:
checkValueOfVariable(new ReturnHandler() {
public void onReturn(Boolean value) {
valueIndicator = value;
}
)
Hope that helps.
Upvotes: 1