简体   繁体   中英

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.

Why don't you create a new service method that returns all the data as a result?

The implementation of such a method could simply call all of the other methods. You will have to encapsulate all the required data and return it as a single result. One example how you could handle this:

In the service implementation:

@Override
public Data getAllData(){

    List<Cars> cars = this.getAllCars();
    List<School> schools = this.getAllSchools();

    return new Data(cars, schools);
}

And you can then use the method like this:

service.getAllData(new AsyncCallback<Data data>() {
    @Override
    public void onSuccess(Data data) {
        fillCarListBox(data.getCars());
        fillSchoolListBox(data.getSchools());
    }

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

With this kind of approach you minimize the number of service calls on your client side. This not only creates a more readable code, but also usually speeds up the client side of your app. You should always try to minimize the number of service calls, ideally to a single one.

Concerning the more general question of collecting a number of asynchronous callbacks, a good approach is to use the Command Pattern . Gwt Remote Action is a library that provides an implementation of the mentioned pattern for doing RPC calls:

http://code.google.com/p/gwt-remote-action/

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;
}

...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM