1

I use gwt+java. Application is working with several databases. The problem is that the query is faster than the process database change that is the reason why data is incorrect in ui. How could I do a delay?

reps.addChangeHandler(new ChangeHandler() {
    public void onChange(ChangeEvent event) {
    final String repPath = reps.getValue(reps.getSelectedIndex());
        RootPanel.get("loadingbarImg").setVisible(true);    
        reps.setEnabled(false);
//next line change database
        serverCall("changeRep" + repPath);
 //then I update the tables "git" and "bugs"   
        serverCall("git");
        serverCall("bugs");
        reps.setEnabled(true);
    }
}
);

The method serverCall() doesn't return any value. This is client side of gwt so Thread.sleep() is unacceptably.

cadmy
  • 515
  • 12
  • 32

2 Answers2

2

I think you are missing the most important part.

GWT is a client / server application. JavaScript is completely async. Therefore the application will make a async call in serverCall("changeRep" + repPath); The next call may be executed immidiately after this one and before the server has executed your "changerep"-call

If you have to ensure the order, you will need to implement a async callback:

http://www.gwtproject.org/doc/latest/DevGuideServerCommunication.html

update

serverCall("firstaction",new AsyncCallback(){

    public void onSuccess(Void v /*or what ever*/){
        serverCall("secondaction" /*need firstaction to be finished*/, 
                   callBackForSecondaction);
    }

    public void onError(Exception e){
        // Make some errorhandling
    }
});

In this case the second action will be executed only if the firstone was successfully finished. Until Java8 the asynchrone code will look like a puzzle of callbacks

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • excuse me, method serverCall already implements the callback. So do you mean I should do another serverCall to ensure that database is changed? Are the callbacks always implemented in order that they appear in code? – cadmy Mar 12 '14 at 21:00
  • method serverCall do the different async callbacks according to the string parameter. So I can add the checking callback. – cadmy Mar 12 '14 at 21:05
1

You should define individual callbacks for the consecutive requests you make to the server. Because you will never know which request gets handled first and returns back to the client. It depends on the kind of processing you do at your server side. So to answer your question, callbacks are not implemented in the order in which they appear in the code.

As a hint, your server call method should be something like this:

void serverCall(String inputParam) {
   if(inputParam.startsWithText.equals("changeRep")
       processChangeRepInput(inputParam);
}

And the functions processChangeRepInput should define a callback on whose onSuccess method, you can make a call to another method, say processGitInput(), which inturn defines its own callback and in its onSuccess method you can make a call to any next server side database update that you would want to do.

somesh
  • 2,509
  • 3
  • 16
  • 24