Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gwt rpc AsyncCallbak之后的代码将不会执行?_Gwt_Rpc - Fatal编程技术网

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

Gwt rpc AsyncCallbak之后的代码将不会执行?,gwt,rpc,Gwt,Rpc,我不明白为什么gwt rpc AsyncCallback之后的代码不会被执行 例如,我的接口AppService扩展了RemoteService,因此我将使用AsyncAppService执行异步调用 下面的代码 AppServiceAsync service = GWT.create (AppService.class); service.getCurrentUser(new AsyncCallback<Employee>(){

我不明白为什么gwt rpc AsyncCallback之后的代码不会被执行

例如,我的接口AppService扩展了RemoteService,因此我将使用AsyncAppService执行异步调用

下面的代码

            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)的计划:

  • 你想打电话购买500阿尔巴尼钢铁公司
  • 通过将在交易大厅传阅的呼叫板
  • 当董事会带着报价返回,且您同意报价时, 接近报价人购买股票
  • 我要喝咖啡

  • 倒咖啡
  • 喝咖啡
  • 需要注意的是:接到电话至少需要10分钟,甚至一个小时。它是异步的。你知道需要多长时间,但不确定

    这是你早上的计划:

  • 执行BuyAlBanySteel(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)的计划:

  • 你想打电话买一辆500美元的吗