我无法理解gwt rpc AsyncCallback之后的代码为什么不会被执行?例如我有接口AppService扩展RemoteService,所以我会AsyncAppService可以完成异步调用。
以下代码
AppServiceAsync service = GWT.create(AppService.class); service.getCurrentUser(new AsyncCallback< Employee>(){ public void onFailure(可捕获的Throwable){ } public void onSuccess(Employee result){ currentUser = result; } }); //如果在上述调用之后有代码,这些代码将不会执行,问题 //如果它们在相同的函数中,则不执行代码。 boolean isAdmin = false; (currentUser!= null){ if(currentUser.getUserRole()。equals(ROLE_ADMIN)|| currentUser.getUserRole()。equals(ROLE_MANAGER)){ isAdmin = true; $ / code $ / pre感谢您的解释
解决方案您应该了解Async调用的本质。当您调用 service.getCurrentUser 时,程序执行不会等待。该程序将继续到下一行( boolean isAdmin = false ),在一段时间内会是真的( currentUser == null)直到方法 getCurrentUser 正在执行。您应该将未执行的代码块移入 onSuccess 处理程序中。
这个例子应该如下所示:
service.getCurrentUser(new AsyncCallback< Employee>(){ public void onFailure(Throwable caught){ $ b public void onSuccess(Employee result){ currentUser = result; if(currentUser!= null){ if (currentUser.getUserRole()。equals(ROLE_ADMIN)|| currentUser.getUserRole()。equals(ROLE_MANAGER)){ isAdmin = true; } } } });我假定currentUser和isAdmin是类字段,但不是局部变量。如果 isAdmin 是本地的,则可以将此变量包装到最终数组中: final boolean [] isAdmin = new boolean [1] 并将它称为 isAdmin [0]
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 AsyncAppService which does the async call.
the following code
AppServiceAsync service = GWT.create (AppService.class); service.getCurrentUser(new AsyncCallback<Employee>(){ public void onFailure(Throwable caught) { } public void onSuccess(Employee result) { currentUser = result; } }); // if i have the code after the above call, these code will not be execute, what is the problem //code following will not be executed if they are in the same function. boolean isAdmin = false; if(currentUser!=null){ if(currentUser.getUserRole().equals("ROLE_ADMIN") || currentUser.getUserRole().equals("ROLE_MANAGER")){ isAdmin = true; } }Thanks for your explaination
解决方案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<Employee>(){ 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]
更多推荐
Gwt rpc AsyncCallbak后的代码不会被执行?
发布评论