2014-01-24 55 views
0

我在GWT中的RPC调用有问题。 下面是一个示例代码片段:GWT-异步呼叫延迟

我在我的UI中有四列,effdate和enddate作为列标题。 这些值不按顺序显示。第一列值显示在第三列中。 大部分时间订单改变。随机显示。

我想每个计数的RPC调用服务器端方法是延迟。 我发现通过调试服务器端方法,我第一次循环传递的effdate和结束日期有时会执行2nd或3rd。这是洗牌。

任何人都可以帮助我。在我的代码中需要做什么更改才能在UI中显示正确的值。 对此有何想法?

count=4; 
    List finalList = new arrayList(); 
    for(i=0;i<count;count++) 
    { 
    effdate= list.get(countincr); 
    enddate= list.get(countincr+1); 
    //call to server side method to fetch values from DB by passing effdate and end date as parameter. 
    grp.ratecalc(startdate,enddate,new AsyncCallback<List<grVo>>() 
    { 
    public void onfailure(throwable caught) 
    { 
    sys.out.print("failure"); 
    } 
    public void onsuccess(List<grVo> result) 
{ 
List grpList= new GrpVO(); 

rate = result.get(0).getrate(); 
rate1 = result.get(0).getrate1(); 

grpList.setrate(); 
grpList.setrate1(); 

setting values in bean for the remaining values in the result. 

finalList.add(grpList); 


if(finalList.size()== count) 
{ 
//calling a method to populate the values 
methoddisplay(finalList); 

} 
} 
} 
); 
countincr+=2; 

} //end of for loop` 
+0

您的代码无法工作。您将RCP呼叫视为同步,但感染它是异步的。这是不确定的四个电话中的哪一个将是第一个接收来自服务器的响应。你必须重新设计你的代码。 –

+1

同步和异步调用有区别。如果你明白这一点,你的问题就会解决。 –

+0

谢谢!我刚开始使用GWT。我明白,一旦请求发生异步,调用就不会等待响应。上面的代码有效,但显示的顺序是错误的。如何使它按正确的顺序执行?我必须循环许多未知数量的Async调用并按正确的顺序执行,我该怎么做? – Minnie

回答

0

您可以使用地图而不是您的finallist来跟踪请求和响应。
我对你的代码做了一些修改(其不完整的plz做了必要的修改)。

int count=4; 
Map<Integer,Object> map = new HashMap<Integer, Object>(); 
for(int i=0;i<count;count++) 
{ final int index=i; 
    effdate= list.get(countincr); 
    enddate= list.get(countincr+1); 
    grp.ratecalc(startdate,enddate,new AsyncCallback<List<grVo>>() 
      { 
     public void onfailure(throwable caught) { 
      sys.out.print("failure"); 
     } 
     public void onsuccess(List<grVo> result){ 
      List grpList= new GrpVO(); 
      rate = result.get(0).getrate(); 
      rate1 = result.get(0).getrate1(); 
      grpList.setrate(); 
      grpList.setrate1(); 
      map.put(index, grpList);     
      //check all items are put on the map and populate values. 
     } 
      } 
      ); 
    countincr+=2; 
} 

这里index用于标识请求和响应。
所有响应GrpVO对象都与其索引一起放入地图。
最后,您必须从地图生成最终列表。
希望这个工程。