开发者

Problem in AsynCallBack - GWT

I have a problem. I am calling to server side on GWT. The results of call is an ArrayList (result). Inside onSuccess method another ArrayList is wrapped with the result. However, if I want to use ArrayList lista outside of onSuccess method it contains 0 elements, but inside is like result ArrayList. How can I solve it? I have tried to use lista as class attribute, static attribute,... but it doesn't run.

Well, I have changed my code, but it doesn't run. I call another method when onSuccess is called...

public void addContainers() throws Exception {
    gwtService.obtainAttributesDevice(1, new AsyncCallback<ArrayList<String>>(){
        @Override
        public void onFailure(Throwable caught) {
            System.out.println("ERROR");
            Window.alert(caught.getMessage());
        }
        @Override
        public  void onSuccess(ArrayList<String> result) {
            lista.addAll(0, result);
            addProjectContainers();
        }       
    });

private void addProjectContainers() throws Except开发者_运维问答ion {
    RootPanel.get("mainContainer").add(new Label("Array lista 2: "+lista.toString()));
    RootPanel.get().add(new Label(String.valueOf("Array lista 2: "+lista.size())));
    for(int i = 0; i < lista.size(); i++){
        RootPanel.get().add(new Label("BOTON "+i));
    }
}


If it helps think of the content of onSuccess and onFailure to be threads that run separately from the rest of the code (it's not really a thread as such, but it's not run in synchronously with the rest of the code, thats why it's called "asynchronous").

It may be help to keep things simpler and avoid massive indenting and also reduces the amount of finals you need to define, if you create a final or member variable referencing this in the class doing an asynchCall that the callback can call a method on to handle the response.

A pseudo code sample (very simplified):

class foo{
   protected callingClass = this;

   void fetchData(){
       service.getData(new Callbac(){
           onSuccess(List<Data> data){
              callingClass.processData(data);
           }
       }
   }
   void procesData(List<Data> fetchedData){
       // Add data etc..
   }
}

EDIT About lots of code after the call:

If you need to do lots of "stuff" after the callback, just put it in the onSuccess or the method that you make onSuccess call (such as in my sample).

If you need to make several callbacks in sequence (where reordering them would cause errors) you should first concider making a service method that does combines those several actions (if possible), or you should do another server call inside the onSuccess callback. To simplify this, avoiding very deeply nested code, you could use a custom callback interface to define an anonymous class to use in the first callback that calls the next server call.

I often use this interface to define something to be done after a callback using this interface (or variations of this):

public abstract class AfterCompleteAction {
    /**
     * The stuff to do when the method receiving this callback is finished.
     */
    public abstract void run();
}


I think you need to read up more on Asynchronous calls - see here.

The problem here is, your code continues while GWT is fetching the values of the ArrayList. You cannot use the contents of the ArrayList until GWT has returned with the data - otherwise, as you have discovered, there will be no data to use.


Please read the funny and informative Great Beer Analogy:

https://groups.google.com/forum/#!topic/google-web-toolkit/-soVdfMGug8

This should explain the problem you're having Jose.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜