#Dealing with pages that load data asynchronously
Introduction
In most cases, your pages will not just be static. You will probably execute some type of asynchronous behavior to retrieve data associated with the page like calling an RPC.
By default, the application presenter will render the page after the code in the onEnterPage method has completed assuming an exception wasn't thrown. This isn't ideal for pages that don't have they data they need to display. GWT Pages provides a callback to handle these types of situations.
You simply need need to call waitForAsync() on the AsyncPageCallback that is provided in the onEnterPage method. This will cause the response cycle to essentially stop and wait until another method is executed on that callback. Usually this would be onSuccess() but could be on onFailure(Throwable) for example or some of the other provided methods.
Examine the differences between these 2 pages:
The following will cause the page to render instantly:
...
public void onEnterPage(PageParameters parameters,
PageRequestSession session, AsyncPageCallback callback) {
// load the page
}
...The following will halt the page loading until the asynchronous data has been retrieved
...
public void onEnterPage(PageParameters parameters,
PageRequestSession session, final AsyncPageCallback callback) {
callback.waitForAsync();
myService.doSomething(..., new AsyncCallback<MyClass>() {
onSuccess(MyClass myClass) {
// load the page
callback.onSuccess();
}
onFailure() {
callback.onFailure();
}
});
}
...