开发者

GWT Async call execution order problem

I have a problem with a throw execution in GWT. So I want to show a simple String list in Client side located on "Server Side". Ok, I have in my Main class this atributes:

private final GreetingServiceAsync greetingService = Util.getInstance(); // This is like > typing GWT.create( GreetingService.class ); public ArrayList songs = new ArrayList();

and in my onModuleLoad() method I have a call to another private method that make the Async call to the Server class:

songs.addAll(getSongsList());

So my getSongsList method is as follow:

public ArrayList<String> getSongsList() {
        final int defaultSize = 4;
        final ArrayList<String> temp = new ArrayList<String>();

        GWT.log("Enter in getSongsLists");
        greetingService.greetSongMostPopular(defaultSize,
                new AsyncCallback<ArrayList<String>>() {

                    public void onSuccess(ArrayList<String> result) {
                        GWT.log("Result is:" + result);
                        temp.addAll(result);
                        GWT.log("Case 1 TEMP= " + temp);
                    }

                    public void onFailure(Throwable caught) {
                  开发者_开发问答      // throw new
                        // UnsupportedOperationException("Not supported yet.");
                        Window.alert("Error greeting data");

                    }

                });
        GWT.log("CASE 2 TEMP = " + temp);
        return temp;

    }

My problem is that in Case 1 I get

[INFO] [MainModule] - Case 1 TEMP= [Song 1, Song 2, Song 3, Song 4]

but in CASE 2 I get the ArrayList empty!!!

[INFO] [MainModule] - Case 1 TEMP= []

What am I doing wrong?

Thanks in advance!


The call to greetSongsMostPopular is asynchronous. This means that the call begins and the code continues directly to the next line of code, in your case GWT.log("CASE 2 TEMP..."). When this line of code is executed, the results are not yet available and you end up with an empty list.

Later, in the background, onSuccess is called with the results. At this point you should call a function to process the results. You can use a pattern similar to the AsyncCallback class, with an onSuccess and onFailure so the caller of this code can handle both cases, or you can just have the caller pass in their own AsyncCallback instance which would make it a thin wrapper around your greetSongsMostPopular RPC function.


This is normal : the callback is called when your server send the answer but the greetSongMostPopular return imediatly.

The call is asynchronous.

All code must be done in the callback (call a function)

Here is the exact process :

  • greetingService.greetSongMostPopular is called
  • temp is empty
  • the second GWT.log is called : temp is always empty
  • the getSongsList method return : temp is always empty
  • the server send the answer to the callback : temp is filled


You are not doing anything wrong. This is expected since you are not dealing with synchronous calls. So the case 2 is only invoked after the asynchronous call returns from the server side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜