tags:

views:

28

answers:

1

I'm developing a Flex application and am having some trouble working with asynchronous calls. This is what I would like to be able do:

[Bindable] var fooTypes : ArrayCollection();

for each (var fooType : FooType in getFooTypes()) {
    fooType.fooCount = getFooCountForType(fooType);
    itemTypes.addItem(fooType);
}

The issue I'm running into is that both getFooTypes and getFooCountForType are asynchronous calls to a web service. I understand how to populate fooTypes by setting a Responder and using ResultEvent, but how can I call another service using the result? Are there any suggestions/patterns/frameworks for handling this?

+2  A: 

If possible, I Strongly recommed re-working your remote services to return all the data you need in one swoop.

But, if you do not feel that is possible or practical for whatever reason, I would recommend doing some type of remote call chaining.

Add all the "remote calls" you want to make in array. Call the first one. In the result handler process the results and then pop the next one and call it.

I'm a bit unclear from your code sample when you are calling the remote call, but I assume it part of the getFooCountForType method. Conceptually I would do something like this. Define the array of calls to make:

public var callsToMake : Array = new Array();

cache the currently in process fooType:

public var fooType : FooType;

Do your loop and store the results:

for each (var fooType : FooType in getFooTypes()) {
    callsToMake.push(fooType);
    // based on your code sample I'm unclear if adding the fooTypes to itemTypes is best done here or in the result handler
    itemTypes.addItem(fooType);
}

Then call the remote handler and save the foo you're processing:

fooType = callsToMake.pop();   
getFooCountForType(fooTypeToProcess);

In the result handler do something like this:

// process results, possibly by setting 
fooType.fooCount = results.someResult;

and call the remote method again:

fooType = callsToMake.pop();   
getFooCountForType(fooTypeToProcess);
www.Flextras.com