Prevent method call until Async Call Completes GWT-Platform
Asked Answered
F

4

4

In my GWT-Platform application I have implemented method in which one step is to fetch data from server and next step is dependent on it. I want to prevent my method for further execution of the code until the Async call completes.

Should be something simple but I am not finding a way.

Frodeen answered 18/6, 2012 at 15:59 Comment(0)
P
6

I think you are missing the point about the web being asynchronous.

It is not considered good practice (it is rather an anti-pattern) to block the execution of your client side code until the async call is finished.

So instead of blocking the execution until your async code is finished do following:

  1. Create a Event which is fired on the global Eventbus when your async code is finished
  2. Attach a Handler for this event in one of your Presenters
  3. Start the async code
  4. Show a loading indicator
  5. When the async call is finished hide the loading indicator and fire the Event on the Eventbus
  6. Handle the next step in the Handler you created before.
Periodicity answered 19/6, 2012 at 12:36 Comment(0)
B
2

I'm not GWT guru, but i'm know how do it in simple way. I would be very grateful if someone tell how to do it the right way, because I was interested in it too. You can just make method which will contain required code and calls it onSuccess or do something like this:

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class Aaa implements EntryPoint {

    private final MyServiceAsync service = GWT
            .create(MyService.class);
    private Timer t;

    public void onModuleLoad() {
        t = new Timer() {
            @Override
            public void run() {
                // Make something or call function
                Window.alert("Next Step");
            }
        };
        final Button sendButton = new Button("Send");
        sendButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                service.sendInfo("Send First Step Info",
                        new AsyncCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                Window.alert("Success Call");
                                // Call Next step code or function
                                t.schedule(1);

                            }

                            @Override
                            public void onFailure(Throwable caught) {
                                Window.alert("Failure Call");
                            }
                        });
            }
        });
        RootPanel.get().add(sendButton);
    }
}
Bader answered 18/6, 2012 at 16:27 Comment(1)
Hi Taras, Kindly consider follwing code snipet and let me know how it will work as you are suggesting, public void someMethod(){ step1; step2; // Here comes an async call step3; // I want to perform this step only after step2 completes, What should I tell GWT to wait until async call complete? } I know we are going against the asyncronous behavior but my use case needs this.Frodeen
D
1

Why using a Timer?

final Button sendButton = new Button("Send");
sendButton.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        service.sendInfo("Send First Step Info",
                new AsyncCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Window.alert("Success Call");
                          nextStep(result);
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Failure Call");
                    }
                });
    }
});

private void nextStep(String data) {
}
Demetrademetre answered 19/6, 2012 at 10:31 Comment(0)
C
0

I had to change the original flow, basically move it and chain to the Async call's onSuccess().

Originally, the flow is

  1. user click a poopup dialog's Ok button
  2. validate (everthing is front end validation), if on validation error, throw exception and show error. diaglog is not closed.
  3. On success validation, proceed with data processing and close the dialog.

Now In a new scenaro, the #2 validate is changed to require a Async call back to the back end. So #3 has to be moved to chain to the callback on the validate method. ' Code snippets below

public void onValidationDataReady(List<Long> existingTests) throws ValidationException {

    if (!existingTests.isEmpty()) {
        throw new ValidationException("The entered name has already been used.");
    }

    //only after  the validation do we proceed with the original OK click 
    proceedOkClick(testNameField.getValue());
}

public void proceedOkClick(String data) {
    // proceed only after the async call

    if (callback != null) {
        callback.onDialogResult(true, data);
    }

    clearDialog();
}


public boolean validateInputs() throws ValidationException {
    //async call to get data from back end.
            //pass in a Callback 
    testNameValidator.validate(testNameField.getValue(), new DataReadyCallback<List<Long>>() {
        @Override
        public void onDataReady(List<Long> existingTests) {
            onValidationDataReady(existingTests);
        }
    });
    return true;
}

//The call back interface. 
public interface DataReadyCallback<T> {
void onDataReady(T data);
}
Cristobalcristobalite answered 29/7, 2013 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.