using AsyncToken and AsyncResponder

562 Views Asked by At

I wanna get two level results of an hierarchical structure. First results of AsyncToken is Ok,then I set a for loop and calling next level results by the same way in each loop step. Here is the problem, token2.addResponder(responder2); never return results on time,on each loop step. It sends me results after all for loop results. But I need in each step . Can you please tell me about my wrong ?

public function getChildResultHandler(event:ResultEvent, token:Object=null ):void
{
        myObject=event.result;  //first asynToken result is Ok
        var myArrayCol:ArrayCollection=new ArrayCollection();               
        myArrayCol=ArrayCollection(myObject);
        var mlObject:MLObject=new MLObject();  

        var i:int;
        for(i=0;i<myArrayCol.length;i++)
        {
            mlObject=myArrayCol[i]; 

            if (mlObject.Type=="Bin")
            {
                token2=new AsyncToken(null);
                token2=myService.GetChildObjects(sessionID,mlObject.ObjectID);
                responder2=new  AsyncResponder(getNextLevelChild,getChildFaultHandler);
                token2.addResponder(responder2);
            // I cant get results here 
            // this query goes to getNextLevelChild func after for loop results
            }
        }
    } 

Do I have to wait for both results, if yes, how can I wait the responder or token ?

2

There are 2 best solutions below

0
nadir.shpz On

Because, these are async calls. It's not possible to know order of returned results. May be first call will end first, may be second one.

So, we have to wait the finishing time of all async calls for correct results.

1
Brent On

This tutorial might help because it solves a problem very similar to what you're experiencing. There are 3 remote procedure calls made passing in the letters ABC. The calls are returned in the order BAC. The tutorial demonstrates how to use ASyncToken and Responders to work out which result handler is firing for which call.