sangupta
sangupta

Reputation: 2406

Synchronous calls using RemoteObject

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

Upvotes: 2

Views: 12299

Answers (8)

Rafael Lucio
Rafael Lucio

Reputation: 1

Maybe if you call a synchronous XMLHttpRequest calling JavaScript on Flex, you can do this.

Upvotes: 0

smagno
smagno

Reputation: 1

You all are somehow mistaken or not using flex from adobe, if you send 2 calls to the server, no matter if each has an individual resquestObject the second one will ONLY be returned after the first one finish, even if the second one takes 1 milisecond to process. Just try the fibonnaci 1/40 example.

Upvotes: 0

sangupta
sangupta

Reputation: 2406

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.

Upvotes: 1

James Ward
James Ward

Reputation: 29433

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));

Upvotes: 5

janetsmith
janetsmith

Reputation: 8732

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.

Upvotes: 2

kenneth
kenneth

Reputation: 1065

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.

Upvotes: 0

CookieOfFortune
CookieOfFortune

Reputation: 14004

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
{
    ...
}

Upvotes: 1

Niclas Adlertz
Niclas Adlertz

Reputation:

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.

Upvotes: 0

Related Questions