Select to view content in your preferred language

Performing Synchronous queries in Flex

683
0
04-16-2014 12:17 PM
AndrewBlakey
Deactivated User
Hi all:

I've become so used to asynchronous queries that when I came across a case where I need it to behave synchronously, I don't know what to do!

My attempted code is below. The problem is that the code gets to the return function before the embedded onResult function fires.  What I want is to query and determine the highest value in a field, then return that value +1.  I know I could do this as separate functions, but that becomes really ugly for the rest of my application.  I want a single function that is called, waits for an answer, then returns the answer.

   private function getNewProjectQuoteId():int
   {
    var query:Query = new Query();
    query.where = "projectquoteid > 0";
    query.outFields = ["projectquoteid"];
    
    queryProjectQuotesTask.execute(query, new AsyncResponder(onResult, onFault));
    
    var previousInt:int = -1;
    
    function onResult(fSet:FeatureSet, token:Object = null):void
    {
     for (var x:int = 0; x < fSet.attributes.length; x++)
     {
      if (fSet.attributes.projectquoteid > previousInt)
      {
       previousInt = fSet.attributes.projectquoteid;
      }
     }
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     Alert.show(info.toString());
    }
    return previousInt + 1

   }
Tags (2)
0 Kudos
0 Replies