Search code examples
javagwtgwt-rpcgwt-mvp

How to collect a number of asynchronous callbacks?


Are there any techiques to collect a number of gwt-rpc service callback results?

I have a dialog window used to create new or edit existing object. These objects have a number of references to other object. So when user creating or editing an object he may pick one in the ListBox.

public class School {
    private String name;
    private String address;
}

public class Car {
    private String model;
    private String type;
}

public class Person {
    private String firstName;
    private String lastName;
    private School school;
    private Car firstCar;
}

When the dialog window appears on the screen it should request all available values for all referencing fields. These values are requested with AsyncCallback's via gwt-rpc, so I can handle it one-by-one.

service.getAllSchools(new AsyncCallback<List<School>>() {
    @Override
    public void onSuccess(List<School> result) {
        fillSchoolListBox(result);
    }

    @Override
    public void onFailure(Throwable caught) {
        Window.alert("ups...");
    }
});
...
service.getAllSchools(new AsyncCallback<List<Car>>() {
    @Override
    public void onSuccess(List<Car> result) {
        fillCarListBox(result);
    }

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

How to get all result in one place? Thanks.


Solution

  • The best solution would be Command Patter as igorbel said, but if you are beginner you can design for example Bean Container that only contains beans that must be transported at one request.

    For example:

    public class BeanContainer{
        private ArrayList<School> schools = new ArrayList<School>();
        private ArrayList<Car> cars = new ArrayList<Car>;
        private ArrayList<Person> people = ArrayList<Person>();

    public void addSchool(School school){
        this.schools.add(school);
    }
    
    public void addSchoolCollection(ArrayList<School> schools){
        this.schools.add(schools);
    }
    
    public ArrayList<School> getSchoolCollection(){
        return schools;
    }
    
    ...
    

    }