9 Replies Latest reply on Mar 6, 2010 8:30 AM by Scott R. Hamilton

    Synchronization problem; Functions not executing in order specified

    Scott R. Hamilton Level 1

      I have a synchronization problem. Three functions are called
      in this order:


      InsertMenu();                  //Inserts a menu record into the database
      getLastRecordId();           //Gets the primary key of the just inserted menu record
                                           //Stores retrieved key in  a textTextInput control
      PrepareMenuItemInsert(); //Reads stored key from textInput
                                           //Inserts a menu item record, setting a link to
                                           //the menu record using the stored key


      Everything functions but at the point when the  PrepareMenuItemInsert() function
      executes, the textInput control has not yet been updated by the getLastRecordId()
      function and is empty. The link to the menu record ends up being set to 0.


      Obviously these functions are not running sequentially in the order specified.


      Is there any way I can force the order or make the PrepareMenuItemInsert()
      function wat until getLastRecordId() has written the key value into the



        • 1. Re: Synchronization problem; Functions not executing in order specified
          Karl_Sigiscar_1971 Level 3



          Yes, you can use an architectural framework like Cairngorm, create commands where you put your logic and chain them (hopefully, without coupling them together).


          Each command will execute a part of your logic and you will be sure they will be executed in the order you want, one after the other.


          There is a solution with SequenceCommand but there are better approaches (without extending SequenceCommand and without calling nextEvent). Another is to use chained UMEvent instances in the Universal Mind version of Cairngorm.


          Best regards,

          • 2. Re: Synchronization problem; Functions not executing in order specified
            KomputerMan.com Level 1

            I don’t use a frameworks myself.. being a solo coder and all.  But in cases like this when I need a value returned from database call I make my database call expect a return.  After I get that value returned is when I execute my code.  For example this is the methodology I employ…


            // Following code set up my database call.


            <mx:RemoteObject id="DataBaseCall" destination="ColdFusion" source="Your_Data_Handler" >

                  <mx:method name="InsertMenu" result="InsertMenuHandler(event)" />



            // make the call passing whatever parameters you want to in whatever way you want to.  I chose Object… just because…

            public function InsertMenu (MyVars:Object):void






            // following code handles the returned call.  The returned event could be a string, int, array, etc…  The type or returned event dictates how you handle the result.

            private function InsertMenuHandler(event:ResultEvent):void


                  Var:YourVar = event.result: // Whatever type of result is returned

                  InsertYourValue(YourVar);   // Call you insert utility



            There are many ways to skin this cat… Pick your poison!!!


            Have an Ordinary Day...

            Kurtis   ~|:-)



            • 3. Re: Synchronization problem; Functions not executing in order specified
              Scott R. Hamilton Level 1

              Thanks Karl, Thanks ComputerMan,


              actually I think I am doing wht you suggest ComputerMan,

              the second function  - getLastRecordId() only runs when

              the event handler for the insert operation is called. That's



              The problem is that the third function - PrepareMenuItemInsert()

              takes off like greased lightening before the returned value has

              been written to the textInput control.


              I tried using the OnChange event of the textInput to call the

              PrepareMenuItemInsert() function, but the call does not

              occur. Presumably the value has to be set in the GUI by the

              user in order for the OnChange event to be dispatched.



              • 4. Re: Synchronization problem; Functions not executing in order specified
                KomputerMan.com Level 1

                NP Scott... That is the challenge with object oriented languages... how to implement procedural steps in a non procedural language... I have found two methods that work for me.  One is do not call a function until another function returns a value I need.  I.E.  I wouldn't call getLastRecord until after the insert operation returns a value.  Actually I would have the insert operation return that value so I don't have to make another database call.  Why make two when one will do? 


                The other method I use is binding values to data elements like data models.  That way when the result set is returned the control that uses them is updated automatically. 


                In the mean time it looks to me like your code is calling a function before a value is returned so move the call to the handler that deals with the result of the insert record call.


                Have an Ordinary Day...

                KomputerMan   ~|:-)

                • 5. Re: Synchronization problem; Functions not executing in order specified
                  Gregory Lafrance Level 6

                  Try the TextInput valueCommit event, which is dispatched if the value changes due to user or due to programmatic change.


                  Note: in Flex 4 valueCommit will, I believe, only be dispatched for programmatic changes.


                  If this post answers your question or helps, please mark it as such.

                  Greg Lafrance - Flex 2 and 3 ACE certified


                  Flex / AIR Development, Training, and Support Services

                  • 6. Re: Synchronization problem; Functions not executing in order specified
                    Scott R. Hamilton Level 1

                    Thanks again guys,


                    I'll give that a try...




                    • 7. Re: Synchronization problem; Functions not executing in order specified
                      Scott R. Hamilton Level 1

                      Hi Kurtis,


                      you seem to built up a head of experience in writing and reading data

                      to and from a database.


                      My problem is is getting the normal feedback mechanisms to function

                      properly. If I do a normal INSERT INTO table () VALUES()... and follow that

                      with SELECT LAST_INSERT_ID() as LastId; In mySQL, I will get the

                      primary key of the just inserted data record.


                      If I do the by issuing the same two queries back-to-back using the

                      mysqlQuery(...) call to the typical php server side module I get

                      a zero back in LastId. This is very frustrating as this should be a

                      simple and straight forward process.


                      So I've been doing a SELECT MAX(lPrimaryKeyId) just to be able

                      to get a value back but this is a kludge at best. It doesn't always

                      work correctly and it would never work in a multi-user environment.






                      • 8. Re: Synchronization problem; Functions not executing in order specified
                        KomputerMan.com Level 1

                        I pretty much use SQL Server and ColdFusion to handle my backend data and processing but the methodology is basically the same.  The methodology I use is as follows.


                        From Flex: Issue the call to write the data to the database (you already have this).  In Flex make sure that the call to write the data is expecting a returned value (string, int, or an array) from the call.  You already have this too but it is associated to the GetLastID() call.  This data should be returned from the insert call.  After you receive that value then you call the next sequence in the chain of events.  I provided examples of how to make this call in Flex in the previous post.


                        In the back end processing:  You receive the values from Flex to add to the database, I call them ValInt_1 and Val_Str_2 in the example below.  Place a lock on the database to ensure only one instance of your app can write to the database at one time.  Insert the data into the database. Get the value of the new PK from the database and place it into a variable. Release the lock which will commit the data into the database.  Return the variable.  In CF this is how I would do it...


                        <CFFUNCTION NAME="AddData" RETURNTYPE="numeric" access="remote" HINT="Add data and return new PK.">

                              <CFARGUMENT name=" ValInt_1" type="any" required="yes">

                              <CFARGUMENT name=" ValStr_2" TYPE="any" required="yes">


                              <CFTRANSACTION ISOLATION="READ_COMMITTED">


                                    <CFQUERY NAME="AddNewClient" DATASOURCE="#Request.DSN#">

                                          INSERT INTO MyTable (ValInt_1, ValStr_2)

                                          VALUES (<CFQUERYPARAM CFSQLTYPE="CF_SQL_ INTEGER " value="#Var_ ValInt_1#">,

                                          <CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" value="#Var_ ValStr_2#">)



                                    <CFQUERY NAME="GetMax" DATASOURCE="#Request.DSN#">

                                          SELECT Max(DataCnt) As MyMax

                                          FROM   MyTable



                                    <CFSET MyNewPK = GetMax.MyMax>



                              <CFRETURN MyNewPK >



                        Bottom line is the write data and return of the new PK is one call from flex, a call that expects a returned value from your backend processing.  It is the return of the database call where you need to handle your sequence of events.  Remember that a call to any Object or function in Flex seems to fire off its own thread so the timing is nolonger sequential at that point.  So to me at least, the trick is to sequence the calls to other events and if the timing is dependent on a called event then the next sequence needs to be moved to that call.  I.E. If sequence item C can not be ran until A and B are both completed I follow this methodology:


                        function private Set_A():void{





                        function private Set_B(YourPassedVars:YourTypeAsNeeded):void{





                        function private Set_C(YourPassedVars:YourTypeAsNeeded):void{




                        I know it is pretty generic but sometimes that is easier...


                        Have an Ordinary Day...

                        KomputerMan   ~|:-)

                        • 9. Re: Synchronization problem; Functions not executing in order specified
                          Scott R. Hamilton Level 1

                          Thanks a Million Kurtis,


                          for your generous and detailed reply...