Search code examples
actionscript-3apache-flexurlloader

URLLoader loads multi files and the result order is the same as call load()


Since URLLoader is async, how to make sure the order of data from server side is the same as the loader.load() call? In other words, the data order in totalResults is the same order of url-related content?

Following is code snippet:

1.for each(var url in urls) {
    loadData(url);
}
2.private function loadData(url:String):void {
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, completeHandler);
    var request:URLRequest = new URLRequest(url);
    urlLoader.load(request);
}
3.private function completeHandler(event:Event):void {
    var loader:URLLoader = URLLoader(event.target);
    var result:Object = loader.data;
    totalResults.push(result);// suppose totalResults is Array and a property in the class.
}

Solution

  • You can extend URLLoader class functionality.

    dynamic class DynamicURLLoader extends URLLoader { }
    

    Then store data ( in your case probably index ) in loader object, before requesting:

     var urlLoader:DynamicURLLoader  = new DynamicURLLoader();
     urlLoader.index = ...
    

    After response, retrieve that data ( in your case index ):

    var loader:DynamicURLLoader = DynamicURLLoader(event.target);
    totalResults[ loader.index ] = loader.data;