Gwt rpc AsyncCallbak之后的代码将不会执行?
我不明白为什么gwt rpc AsyncCallback之后的代码不会被执行 例如,我的接口AppService扩展了RemoteService,因此我将使用AsyncAppService执行异步调用 下面的代码Gwt rpc AsyncCallbak之后的代码将不会执行?,gwt,rpc,Gwt,Rpc,我不明白为什么gwt rpc AsyncCallback之后的代码不会被执行 例如,我的接口AppService扩展了RemoteService,因此我将使用AsyncAppService执行异步调用 下面的代码 AppServiceAsync service = GWT.create (AppService.class); service.getCurrentUser(new AsyncCallback<Employee>(){
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;
}
}
AppServiceAsync service=GWT.create(AppService.class);
service.getCurrentUser(新的AsyncCallback(){
失败时的公共无效(可丢弃){
}
成功时公开作废(员工结果){
当前用户=结果;
}
});
//如果我在上面的调用之后有了代码,这些代码将不会被执行,有什么问题吗
//如果它们在同一个函数中,则不会执行以下代码。
布尔值isAdmin=false;
如果(currentUser!=null){
if(currentUser.getUserRole().equals(“角色管理”)||
currentUser.getUserRole().equals(“角色管理器”)){
isAdmin=true;
}
}
感谢您的解释您应该了解异步调用的本质。调用
service.getCurrentUser
时,程序执行将不会等待。程序将继续到下一行(boolean isAdmin=false
),并且在执行方法getCurrentUser
之前,(currentUser==null)
将在一段时间内为真。您应该将未执行的代码块移动到onSuccess
处理程序中
此示例应如下所示:
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;
}
}
}
});
service.getCurrentUser(新的AsyncCallback(){
失败时的公共无效(可丢弃){
}
成功时公开作废(员工结果){
当前用户=结果;
如果(currentUser!=null){
if(currentUser.getUserRole().equals(“角色管理”)||
currentUser.getUserRole().equals(“角色管理器”)){
isAdmin=true;
}
}
}
});
我假设currentUser和isAdmin是类字段,但不是局部变量。如果
isAdmin
是本地变量,则可以将此变量包装到最终数组中:final boolean[]isAdmin=new boolean[1]
并像isAdmin[0]
那样调用它,您应该了解异步调用的性质。调用service.getCurrentUser
时,程序执行将不会等待。程序将继续到下一行(boolean isAdmin=false
),并且在执行方法getCurrentUser
之前,(currentUser==null)
将在一段时间内为真。您应该将未执行的代码块移动到onSuccess
处理程序中
此示例应如下所示:
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;
}
}
}
});
service.getCurrentUser(新的AsyncCallback(){
失败时的公共无效(可丢弃){
}
成功时公开作废(员工结果){
当前用户=结果;
如果(currentUser!=null){
if(currentUser.getUserRole().equals(“角色管理”)||
currentUser.getUserRole().equals(“角色管理器”)){
isAdmin=true;
}
}
}
});
我假设currentUser和isAdmin是类字段,但不是局部变量。如果
isAdmin
是本地变量,则可以将此变量包装到最终数组中:final boolean[]isAdmin=new boolean[1]
并将其命名为isAdmin[0]
想象一下,您是过去非计算机化股票/商品市场中的经纪人。让我们假设它以以下方式工作
今天是星期一上午9点30分。你已经按顺序计划好了。事实上,你是如此有经验,它被编入你的体内:
购买Albanysteel(500)的计划:
9.31 am
Send offer(
buy = 500 AlbanySteel
messenger = annie
When annie comes back, analyse offer.
Pour Annie a cup of tea.
)
9.32 am
if (annie has an agreeable offer) buy 500 AlbanySteel.
9.33 am
Pour Coffee.
9.34
Drink Coffee.
你当然不能。原因如下
9.32 am
if (annie has an agreeable offer) buy 500 AlbanySteel.
将无法正确执行。这场演出似乎没有上演,因为安妮还不会带着邀请回来。她可能还需要10分钟或一个小时才能回复报价
因此,这就是您执行工作流程的方式
9.31 am
Send offer(
buy = 500 AlbanySteel
messenger = annie
when annie comes back,
analyse offer.
Pour Annie a cup of tea.
if (annie has an agreeable offer) buy 500 AlbanySteel.
)
9.33 am
Pour Coffee.
9.34
Drink Coffee.
那么,在GWT伪代码中,您会选择执行哪一个
您是否会执行以下操作:
BuyAlbanySteelAsync albanyService = GWT.create(BuyAlbanySteel.class);
albanyService.getOffer(
new Task<Annie>(){
onFailure(Annie){Do nothing}
OnSuccess(Annie){
analyse offer.
Pour Annie a cup of tea.
}
}
);
if(Annie has agreeable offer)
buy the stock.
BuyAlbanySteelAsync albanyService=GWT.create(BuyAlbanySteel.class);
albanyService.getOffer(
新任务(){
失败(安妮){无所事事}
成功(安妮){
分析报价。
给安妮倒杯茶。
}
}
);
如果(安妮提出了令人满意的提议)
买股票。
或者这个:
BuyAlbanySteelAsync albanyService=GWT.create(BuyAlbanySteel.class);
albanyService.getOffer(
新任务(){
失败(安妮){无所事事}
成功(安妮){
分析报价。
给安妮倒杯茶。
如果(安妮提出了令人满意的提议)
买股票。
}
}
);
想象一下,你是过去非计算机化股票/商品市场的经纪人。让我们假设它以以下方式工作
今天是星期一上午9点30分。你已经按顺序计划好了。事实上,你是如此有经验,它被编入你的体内:
购买Albanysteel(500)的计划: