Clean way in GWT/Java to wait for multiple asynchronous events to finish

前端 未结 8 868
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:32

What is the best way to wait for multiple asynchronous callback functions to finish in Java before continuing. Specifically I\'m using GWT with AsyncCallback, but I think this i

相关标签:
8条回答
  • 2021-01-30 14:08

    Ideally, you want to do as other posters have stated and do as much as you can in a single async call. Sometimes you have to do a bunch of separate calls. Here's how:

    You want to chain the async calls. When the last async completes (login), all the items are loaded.

        final AsyncCallback<LoginInfo> loginCallback = new AsyncCallback<LoginInfo>() {
            public void onSuccess(LoginInfo result) {
                //Everything loaded
                doSomethingNow();
            }
        };
        final Runnable searchRunnable = new Runnable() {
            public void run() {
                loginService.login(GWT.getHostPageBaseURL(), loginCallback);
            }
        };
    
        final Runnable booksRunnable = new Runnable() {
            public void run() {
                AjaxLoader.loadApi("search", "1", searchRunnable, null);
            }
        };
    
        //Kick off the chain of events
        AjaxLoader.loadApi("books", "0", booksRunnable, null);
    

    Cheers,

    --Russ

    0 讨论(0)
  • 2021-01-30 14:09

    Just tossing up some ideas:

    The callbacks fire some GwtEvent using the HandlerManager. The class containing the ready methods is registered with the HandlerManager as an EventHandler for the events fired by the callback methods, and holds the state (bookAPIAvailable, searchAPIAvailable, appLoaded).

    When a event arrives that specific state is changed, and we check if all the states are as desired.

    For an example using the GWTEvent, HandlerManager and EventHandler, see http://www.webspin.be/?p=5

    0 讨论(0)
提交回复
热议问题