On Wed, Apr 15, 2009 at 1:22 PM, badgerduke <[email protected]> wrote: > Just started using GWT and I am having difficult tailoring my code to > the asynchronous nature of RPC calls. Specifically, my code asks for > the results of an RPC call before anything is returned. > > Here is my class which calls a service. Instead of using an inner > class for the AsyncCallback, I use a separate implementation, > FormAsyncCallback, whose code is also below: > > > package com.gallup.client; > > import com.google.gwt.core.client.GWT; > import com.google.gwt.user.client.rpc.AsyncCallback; > import com.google.gwt.user.client.rpc.ServiceDefTarget; > > public class FormService { > > public String call(FormBean formBean) throws FormException{ > > FormRemoteServiceAsync serviceProxy = (FormRemoteServiceAsync) > GWT.create(FormRemoteService.class); > ServiceDefTarget target = (ServiceDefTarget) serviceProxy; > > String serviceURL = GWT.getModuleBaseURL() + "formservice"; > if (GWT.isScript()) { > serviceURL = "/Struts2GWT/formservice"; > } > target.setServiceEntryPoint(serviceURL); > > final String successMessage = null; > > FormAsyncCallback callback = new FormAsyncCallback(); > > serviceProxy.setForm(formBean, callback); > > if (!callback.isHadError()) { > GWT.log("AAAAA", null); > return callback.getSuccessMessage(); > } > else { > throw new FormException(callback.getErrorMessage()); > } > } > > } > > > package com.gallup.client; > > import com.google.gwt.core.client.GWT; > import com.google.gwt.user.client.rpc.AsyncCallback; > > public class FormAsyncCallback implements AsyncCallback { > > > private String successMessage = null; > private boolean hadError = false; > private String errorMessage = null; > > public void onFailure(Throwable t) { > hadError = true; > GWT.log("BBBBBBBB", null); > errorMessage = t.getMessage(); > } > > public void onSuccess(Object result) { > GWT.log("CCCCCCCCC", null); > successMessage = (String) result; > > } > > public String getSuccessMessage() { > return successMessage; > } > > public boolean isHadError() { > return hadError; > } > > public String getErrorMessage() { > return errorMessage; > } > public boolean isReturned() { > return returned; > } > > } > > > > My problem is that "AAAAAAA" always prints before "BBBBB" or > "CCCCCCC". I really want my code to wait for the RPC call to return > before I interrogate the results. Does anybody have suggestions?
Change your mind. No, seriously. Read http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/faca1575f306ba0f?pli=1 and then figure out how to redesign your app to be asynchronous. Chances are you're going to want to pass an instance of an anonymous inner class as the AsyncCallback and have it delegate in onSuccess or onFailure to "business logic" somewhere else. One thing that might help you is to think of RPC in an event-oriented manner. You're presumably already comfortable handling button-clicks and widget-focuses. Start thinking about RPC responses as events that happen "sometime after" their corresponding requests. Build your app around this line of thought and your life will be much easier. Perhaps more importantly, your user's lives will be at least tolerable (which they won't be if you force your RPCs to be synchronous). Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Web-Toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
