views:

3500

answers:

7

Is there a way to make synchronous calls using RemoteObject in Flex?

Solution: Add the second call to the result handler of the first call, having a token check for multiple originating calls.

A: 

No, why would you wish to do that anyway. Flex makes things asynchronous so that the user isn't forced to sit and wait while data is coming back. It would be a very poor user expereince if each time an app requested data the user had to wait on it coming back before anything else could happen.


from comment

No you don't need synchronus behaivour. If you're making say 2 calls and call 2 comes in before call 1, but 2 relies on the data inside 1 then you're left with either don't fire off event 2 till 1 comes back (this will slow down your app - much like synchronus events) or implement a way to check that event 1 has come back in event 2's handler (there are many ways you could do this). If you're firing off many events then why not have a wrapper class of some description that tracks your events and doesn't do anything on the responses until all events are back. You can use the AsyncToken to keep track of individual requests, so if you are firing of loads at once then you can find out exaclty whats come back and whats not.

kenneth
Huh - when you need to make multiple remote object calls where the result of one depends on another, you need synchronous behaviour. Adding a facade is not always possible - specifically when server code is not in your hands.
Sandy
+1  A: 

If you want synchronous behavior, just add a wait after you make the call.

EDIT: I've added code for the chaining behavior I was talking about. Just replace the result handler each subsequent time you call the remoteObject.

...
remoteObject.function1(...);
...

private var resultHandler1(event:ResultEvent):void
{
    ...
    remoteObject.removeEventListener(resultHandler1);
    remoteObject.addEventListener(ResultEvent.RESULT, resultHandler2);
    remoteObject.function2(...);
}

private var resultHandler2(event:ResultEvent):void
{
    ...
}
CookieOfFortune
Crap! You can't wait in flex and second, you don't know the timing in which the call would complete.
Sandy
Well, I meant wait as in, do nothing. And of course you don't know the timing, that's why the call is asynchronous. But you can just call the remoteObject again from the resultHandler of one call and chain them in that fashion.
CookieOfFortune
This seems like the proper approach, if one is dependent on the other, merely use the built-in event hierarchy to delegate things on time. Since the second remote objects call happens on the completion of the first, we know that basing the second off of the first is available and accurate.
Brian Hodge
A: 

you should perhaps try and make one request with with all the data u want to be recieved synchronous and then make the different classes that need data listen to the correct data for that class.

ex:

  // request
  remoteobject.GetData(); 

  // on received request
  private function receivedData(evt:ResultEvent):void   
  {

     for each (var resultobject:ResultObjectVO in evt.result)
     {

        var eventModel:Object;

        var event:DataEvents = new DataEvents(resultobject.ResultType);
        event.data          = eventModel;

        eventdispatcher.dispatchEvent(event);
     }
  }

Something like this. Hopes this helps.

+3  A: 

All IO in Flex is asynchronous. The typical pattern to deal with this is to use an AsyncResponder. For instance:

var t:AsyncToken = remoteObject.methodCall();
t.addResponder(new AsyncResponder(resultEvent, faultEvent));
James Ward
An AsyncResponder is used for asynchronous call responses - it can't let you do this in synchronous way :(
Sandy
There is no (clean) way to make IO synchronous in Flex.
James Ward
A: 

think twice when u want it to be synchronous.

Do u know what synchronous mean? it will FREEZE your application until it receive data. Unless u are pretty sure that your remote calling can receive return value immediately (super fast network connection).

if your function call depends on each other, i would suggest you implement a state machine. e.g.

after 1st async call, your state becomes STATE_1, and your next function call will check on this state variable, to decide next move (ignore the current call or carry on).

my 2 cents.

janetsmith
Thanks for the reply. We are on a local company network, but still implemented this using the state machine.Thanks, anyways!
Sandy
A: 

I achieved the same in two ways: First, as said above the use of state machines. It may get tricky at times. Second, the use of command queues - I think this is the best way to do it... but the downside is that the UI may not be very reflective in this time.

Sandy
Can make the UI more friendly to the user by displaying a loading indicator and informative status messages. With Flex this is really easy to do - just bind something in your loading component to the value of a loadingText:String property in your model or presentation model, and just update the loadingText value from your Controller as it executes the consecutive commands or service calls in your queue. Presto!
Karthik
A: 

No, why would you wish to do that anyway. Flex makes things asynchronous so that the user isn't forced to sit and wait while data is coming back.

there are situations when you need exactly this - user needs to sit and wait till the application load all necessary data from the server