code after Gwt rpc AsyncCallbak will not be executed?

前端 未结 2 1140
梦如初夏
梦如初夏 2021-01-25 02:30

I can\'t understand why the code after the gwt rpc AsyncCallback will not be executed?

for example I have the interface AppService extends RemoteService, So I\'ll have A

2条回答
  •  轮回少年
    2021-01-25 03:03

    You should understand the nature of the Async call. The programm execution will not be waiting when you call service.getCurrentUser. The programm will continue to the next line (boolean isAdmin = false) and it will be true for some time that (currentUser == null) until method getCurrentUser is executing. You should move not executed block of code into the onSuccess handler

    This example should look something like this:

    service.getCurrentUser(new AsyncCallback(){
    
        public void onFailure(Throwable caught) {
    
        }
    
        public void onSuccess(Employee result) {
            currentUser = result;
            if (currentUser != null) {
                if (currentUser.getUserRole().equals("ROLE_ADMIN") ||
                    currentUser.getUserRole().equals("ROLE_MANAGER")) {
                    isAdmin = true;
                }
            }
    
        }
    
    });
    

    I assume that currentUser and isAdmin are class fields, but not local variables. If isAdmin is local than you can wrap this variable into the final array: final boolean[] isAdmin = new boolean[1] and call it like isAdmin[0]

提交回复
热议问题