Search code examples
google-app-enginegwtrequestfactory

App Engine GWT code not being executed at the right time


I have an App Engine GWT app. In my client code, I'm getting the logged in users email, and want to display it. I get the users email by a request to the server.

The problem is that the getUserEmail() method is being executed after the onModuleLoad() method has been executed, so that the userEmail-String will turn up as null where i want to display it. I have checked that the getUserEmail()-method actually returns the correct piece of info, my problem, as mentioned, is when i get the info i want. Can anyone point me anywhere on why this happens and how to fix this?

public String userEmail;

@Override
public void onModuleLoad() {
        getUserEmail();
        HTML mHTML = new HTML();
        mHTML.setHTML("<HTML><BODY> Logged in as " + userEmail + "</BODY></HTML>");
        RootPanel.get().add(mHTML);
}

public void getUserEmail(){

    requestFactory.getUserEmailRequest().getUserEmail().fire(
        new Receiver<String>()  {
                @Override
                public void onSuccess(String result) {
                        userEmail = result; 
                    }
                }); 
}

Solution

  • Method getUserEmail() is Asynchronous Method Call. If you want to execute code after callBack then you should write it in the inner class :

    public void getUserEmail(){
    
    requestFactory.getUserEmailRequest().getUserEmail().fire(
        new Receiver<String>()  {
                @Override
                public void onSuccess(String result) {
                        userEmail = result; 
    
                        HTML mHTML = new HTML();
                        mHTML.setHTML("<HTML><BODY> Logged in as " + userEmail + "</BODY></HTML>");
                        RootPanel.get().add(mHTML);
                    }
                }); 
    }