Gwt rpc AsyncCallbak后的代码不会被执行?

编程入门 行业动态 更新时间:2024-10-24 10:25:18
本文介绍了Gwt rpc AsyncCallbak后的代码不会被执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法理解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后的代码不会被执行?

本文发布于:2023-11-12 04:30:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Gwt   rpc   AsyncCallbak

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!