I’ve been wondering if we there is an native or generic way to synchronize multiple results from asynchronous HttpService calls in Adobe Flex. In other words, I need to recover some data from my server and execute an function after i recovered all the data from these different functions.
Here’s an example:
private var mObj01 : Object = null;
private var mObj02 : Object = null;
private var mObj03 : Object = null;
public function TryLoadSynchronousFunction() : void
{
if( mObj01 != null && mObj02 != null && mObj03 != null )
DoSynchronizedStuff();
}
public function GetObj01( event : ResultEvent ) : void
{
mObj01 = event.result as Object;
TryLoadSynchronousFunction();
}
public function GetObj02( event : ResultEvent ) : void
{
mObj02 = event.result as Object;
TryLoadSynchronousFunction();
}
public function GetObj03( event : ResultEvent ) : void
{
mObj03 = event.result as Object;
TryLoadSynchronousFunction();
}
public function StartAsynchronous() : void
{
myCall01.token = httpObj.Get01();
myCall02.token = httpObj.Get02();
myCall03.token = httpObj.Get03();
}
And on the <fx:Declarations> I would have:
<mx:CallResponder id="myCall01" result="GetObj01(event)" />
<mx:CallResponder id="myCall02" result="GetObj02(event)" />
<mx:CallResponder id="myCall03" result="GetObj03(event)" />
Is there a better approach to this kind of situation?
To begin with, CallResponders are ok in demo code (such as the demo that Adobe provides), but in reality – this isn’t a good implementation within an application. You need to create some service delegates that handle all of your interaction with the server:
http://www.developria.com/2008/04/anatomy-of-an-enterprise-flex-8.html
Also, in this case, I would recommend looking at something like the chaining API with the Swiz framework as an option for chaining together a group of asynchronous tasks.
http://swizframework.jira.com/wiki/display/SWIZ/Chaining+API