Search code examples
javagwtgwt-rpc

GWT RPC only handle results from the last call(back)


I'm wondering if there is a cleaner and more re-usable way to implement the following use case:

I have a search form that sends a RPC to the server with the user entered values. The callback returns the items that match the search criteria. A search can take several seconds and we want the user to be able to alter his search criteria even if the previous search hasn't completed yet. If he does this, all previous search results should be ignored and the last result should be presented. If we don't take this in account, sometimes the newest call will return before the older ones resulting in the newest results getting overridden, which means the presented results won't match the currently entered criteria.

We currently solve this by assigning the callback to a "lastCallback" field. Each callback instance can access this field and checks whether the field is equal to itself. If not, a newer search request has been sent in the meantime and thus the results of this old call are ignored.

Does GWT provide a built-in way to handle this use case?


Solution

  • You can cancel the previous calls. Have your async methods return com.google.gwt.http.client.Request instead of void, so you can call cancel() on it. This would make sure your previous callback will never be called and you only ever have one ongoing request (from the client's point of view at least; you'll have several being processed on the server because cancelling a request is a client and network thing, and isn't exposed to the servlet server-side)