You need to sign in to do that
Don't have an account?
Flex AsyncResponders Issue
Hi All,
I have the issue with asynchronous execution of SOQL in Flex. I am trying to execute two SOQL sequentially.
Step 1:
I execute the first SOQL and keep the result in a ArrayCollection.
Step 2:
I execute the second SOQL and keep the result in a Array.
Step 3:
I perform some business logic, and make some changes in the ArrayCollection [initialized in Step 1] on the basis of the values present in the Array [Initialize in Step 2].
Problem:
Because the SOQL in Flex executes in the async mode, Step 3 executes in parallel of Step 2 and Array does'nt have any value for processing.
please have a look into the code.
public static function getAllLocations(sOppId:String, apexConnection:Connection):ArrayCollection{ var locations:ArrayCollection = new ArrayCollection(); var soql:String = "Select l.City__c, l.Country__c, l.Id, l.State__c, l.ZipCode__c from Location__c l"; try{ apexConnection.query(soql, new AsyncResponder( function (qr:QueryResult):void{ if(qr.size <= 0){ // show error }else{ for(var i:int=0; i<qr.records.length; i++ ){ var location:Object = qr.records[i]; locations.addItem({id:location.Id, city:location.City__c, state:location.State__c, country:location.Country__c, zipCode:location.ZipCode__c, choose:false}); } var locIdArray:Array = getAllEnteredOppLocations(sOppId, apexConnection); for(var a:int = 0; i<locIdArray.length; i++){ for(var j:int=0; j<locations.length; j++){ if(locIdArray[a].toString() == locations.getItemAt(j).Id){ locations.getItemAt(j).choose = true; } } } } }, function(fault:Object):void{ Alert.show("Some Fatal Error Occurred............." + fault); } )); }catch(error:Error){ locations.addItem({id:'1', city:'Noida', state:'UttarPradesh', country:'India', zipCode:'201301', choose:true}); locations.addItem({id:'2', city:'Ghaziabad', state:'UttarPradesh', country:'India', zipCode:'301205', choose:false}); } locations.refresh(); return locations; } private static function getAllEnteredOppLocations(sOppId:String, apexConnection:Connection):Array{ var locIdArray:Array = new Array(); var soql:String = "Select Location__c from Opportunity_Location__c o where Opportunity__c = '0068000000LO8g9'"; try{ apexConnection.query(soql, new AsyncResponder( function (qr:QueryResult):void{ if(qr.size <= 0){ Alert.show("Total is less than 0: "); }else{ for(var i:int=0; i<qr.records.length; i++ ){ locIdArray.push(qr.records[i].Location__c); } } }, function(fault:Object):void{ Alert.show("Some Fatal Error Occurred in getAllOppLocations............." + fault); } )); }catch(error:Error){ } return locIdArray; }
Thanks
Ajit
It is something tricky, you can use write the business logic in a third function.
and call this function only when both the calls are returned. we can use a boolean variable to achieve this.
Except it, it is better to write your code using a framework in Flex.i used PureMVC framework to avoid such kind of situation, because in PureMVC framework command are executed on the basis of notifications. so it will be easier to code using PureMVC framework, AjitAll Answers
I have a similar problem with AsyncResponder... did you come up with a solution for this problem?
Thanks,
Dave
It is something tricky, you can use write the business logic in a third function.
and call this function only when both the calls are returned. we can use a boolean variable to achieve this.
Except it, it is better to write your code using a framework in Flex.i used PureMVC framework to avoid such kind of situation, because in PureMVC framework command are executed on the basis of notifications. so it will be easier to code using PureMVC framework, Ajit