Breaking for or loop GWT callback response

2019-08-10 08:25发布

I want to break the for loop in GWT callback's execute method response.

For Example,

for (int idx = 0; idx < recordList.getLength(); idx++) {  //Starting ABC FOR LOOP
    ABCDMI.addData(recordList.get(idx), 
                   new DSCallback() {       
                       public void execute(DSResponse response, Object rawData, DSRequest request) {      
                            if(response.getAttribute("UnSuccess") != null && !response.getAttribute("UnSuccess").equalsIgnoreCase("")) {    
                                 break;  //I want to break ABC FOR LOOP here.   
                            }
                   }
}

Can anybody help me in this?

标签: gwt smartgwt
1条回答
时光不老,我们不散
2楼-- · 2019-08-10 08:56

When you call an asynchronous method, you dont know how long it will take. In your examples all of these calls will be sent in almost the same instant, but the response would come in any time in the future, so the order is not guaranteed.

Of-course you cannot break a loop inside your callback, but you can handle the loop inside your callback calling the async method from it each time one call finishes.

This example should work in your case, and all callbacks would be executed sequentially.

DSCallback myCallBack = new DSCallback() {
  int idx = 0; 
  int length = recordList.getLength();

  public void execute(DSResponse response, Object rawData, DSRequest request) {
    if (++idx < length 
          && (response.getAttribute("UnSuccess") == null 
            || !response.getAttribute("UnSuccess").equalsIgnoreCase(""))) {
      ABCDMI.addData(recordList.get(idx), this);
    }
  }
};

ABCDMI.addData(recordList.get(0), myCallBack);
查看更多
登录 后发表回答