在我的GWT平臺應用程序中,我實現了一步從服務器獲取數據的方法,下一步依賴於它。我想阻止我的方法進一步執行代碼,直到異步調用完成。阻止方法調用,直到異步調用完成GWT平臺
應該是簡單的,但我沒有找到方法。
在我的GWT平臺應用程序中,我實現了一步從服務器獲取數據的方法,下一步依賴於它。我想阻止我的方法進一步執行代碼,直到異步調用完成。阻止方法調用,直到異步調用完成GWT平臺
應該是簡單的,但我沒有找到方法。
我認爲你錯過了關於異步網絡的觀點。
在異步調用完成之前,阻止客戶端代碼的執行並不被認爲是一種好的做法(它更像是一種反模式)。
因此,而不是直到你的異步代碼執行完畢做以下阻止執行:
我不是GWT大師,但我知道它是如何以簡單的方式。 如果有人以正確的方式告訴我如何去做,我會非常感激,因爲我也對它感興趣。 你可以只讓法將包含所需的代碼和調用它的onSuccess或做這樣的事情:
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
public class Aaa implements EntryPoint {
private final MyServiceAsync service = GWT
.create(MyService.class);
private Timer t;
public void onModuleLoad() {
t = new Timer() {
@Override
public void run() {
// Make something or call function
Window.alert("Next Step");
}
};
final Button sendButton = new Button("Send");
sendButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
service.sendInfo("Send First Step Info",
new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
Window.alert("Success Call");
// Call Next step code or function
t.schedule(1);
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure Call");
}
});
}
});
RootPanel.get().add(sendButton);
}
}
爲什麼使用Timer
?
final Button sendButton = new Button("Send");
sendButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
service.sendInfo("Send First Step Info",
new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
Window.alert("Success Call");
nextStep(result);
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure Call");
}
});
}
});
private void nextStep(String data) {
}
我不得不改變原來的流動,基本上和鏈轉移到異步調用的的onSuccess()。
原來,流量
現在在新的場景中,#2驗證更改爲需要將異步回調回到後端。所以#3必須被移動以鏈接到validate方法的回調。「下面
public void onValidationDataReady(List<Long> existingTests) throws ValidationException {
if (!existingTests.isEmpty()) {
throw new ValidationException("The entered name has already been used.");
}
//only after the validation do we proceed with the original OK click
proceedOkClick(testNameField.getValue());
}
public void proceedOkClick(String data) {
// proceed only after the async call
if (callback != null) {
callback.onDialogResult(true, data);
}
clearDialog();
}
public boolean validateInputs() throws ValidationException {
//async call to get data from back end.
//pass in a Callback
testNameValidator.validate(testNameField.getValue(), new DataReadyCallback<List<Long>>() {
@Override
public void onDataReady(List<Long> existingTests) {
onValidationDataReady(existingTests);
}
});
return true;
}
//The call back interface.
public interface DataReadyCallback<T> {
void onDataReady(T data);
}
嗨塔拉斯 代碼片段,笑納follwing一小段代碼片段,讓我知道它是如何工作的,你所提出的建議, 公共無效的someMethod(){ \t第一步; \t \t step2; //這裏是一個異步調用 \t \t step3; //我只想在step2完成後才執行這個步驟,我應該告訴GWT等待異步調用完成? } 我知道我們正在反對異步行爲,但我的用例需要這樣做。 – Bhavesh