Search Flex Components Free

Custom Search

December 15, 2007

Asynchronicity Blues - managing multiple dataservice calls

A while ago I once again came up face to face with the mean and hungry asynchronicity beast that lives within Flex. All I was trying to do was create and save a Contact record programmaticly. The problem was, however, that a Contact has references to ContactType, Country and Division. Simplified, I was trying do the following:

countryService.fill(countries);contactTypeService.fill(contactTypes);divisionService.fill(divisions);contact.country = countries.getItemAt(0); //defaultcontact.contactType = contactTypes.getItemAt(0); //defaultcontact.division = divisions.getItemAt(0); //defaultcontactService.createItem(contact);
Of course, most of the time this would fail because the collections would not yet be filled, being asynchronous in nature. So I had to find a workaround. I created a dataTasks collection and then added a dataTask prior to each fill to the collection. On the result event of each fill I would find my dataTask and remove it from the collection. I added an EventListener to the CHANGE event of my dataTasks collection and when the collection length was back to 0, I would remove the EventListener and create my Contact. This worked fine, however it required alot more coding. Here's what it looked like:
//prior to fillif(contactTypes.length == 0){dataTask = new DataTask("ContactTypes",false);model.dataTasks.addItem(dataTask);}contactTypeService.fill(contactTypes, "flex:hql", "from ContactType");//onResult Eventif(dataTask){ model.dataTasks.removeItemAt(model.dataTasks.getItemIndex(dataTask));dataTask = null;}//add Eventlistener to dataTasks model.dataTasks.addEventListener(CollectionEvent.COLLECTION_CHANGE,completeContact);...private function completeContact(evt:CollectionEvent = null):void{if(model.dataTasks.length == 0) {model.dataTasks.removeEventListener(CollectionEvent.COLLECTION_CHANGE,completeContact);var contact:Contact = new Contact();...Again, I posted on Flexcoders as to whether or not this was the BEST way to do this, and again Mr. Vroom responded. He wrote that even though the calls on the client are made ASYNCHRONOUSLY, the server handles the requests in SYNCHRONOUS order. In other words I only have to listen for the result event of the LAST fill called. When that result is returned to the client, barring any faults from the previous fills, we can assume that all collections have been filled. Yay! Less Code!

Related Flex Tutorials