E. Rowlands
E. Rowlands

Reputation: 343

In a GWT asynchronous callback, how can I handle the same return types in onSuccess?

If I have two booleanmethods in a service, how can I ensure the onSuccess method deals with the correct method that is returning a boolean?

For example, in the onSuccess method I say:

if (result instanceof Boolean) {};

Is there a way that I can differentiate the service method that is returning the boolean? Otherwise it's impossible to guarantee the right code will execute in my onSuccess method, as if the result is a boolean it will execute if either of my two boolean service methods are called.

Here's an example of the problem I'm facing:

private class DefaultCallback implements AsyncCallback
        {
        @Override
        public void onFailure(Throwable caught)
            {
            mainGUI.getServerResponseLabel().setStyleName("serverResponseLabelError");
            mainGUI.getServerResponseLabel().setHTML("An error occurred while "
                    + "attempting to contact the server. Please check your network " + "connection and try again.");
            }

        @Override
        public void onSuccess(Object result)
            {
            if (result instanceof Boolean)
                { 
                //
                I have two methods that return a boolean here, 
                so this block will execute no matter which one is called;
                for one method I want to display a GUI windows saying "Upload complete",          
                and for another, create an excel spreadsheet. But the boolean value of one method won't be relevant to the other

Upvotes: 1

Views: 463

Answers (1)

HBo
HBo

Reputation: 633

There's a lot of grey area, but bear with me: in SomethingRPCService.java

boolean isA();
boolean isB();

in SomethingRPCServiceAsync.java

void isA(AsyncCallback<Boolean> callback);
void isB(AsyncCallback<Boolean> callback);

in your activity

somethingService.isA(new AsyncCallback<Boolean>() {
        @Override
        public void onSuccessImpl(final Boolean response) {
            //will be executed on success of THIS call
        }
        @Override
        public void onFailure(final Throwable caught) {
            // not relevant here
        }
});

somethingService.isB(new AsyncCallback<Boolean>() {
        @Override
        public void onSuccessImpl(final Boolean response) {
            //will be executed on success of THIS call
        }
        @Override
        public void onFailure(final Throwable caught) {
            // not relevant here
        }
});

You can (have to?) actually type your return

Edit : with your example, I can see a little better; your callback shouldn't be the same, you should override it so the code isn't the same, because it isn't

Upvotes: 3

Related Questions