4 Replies Latest reply on May 20, 2010 7:17 AM by NW987

    ArrayCollection Not Populating After First Data Services Call

    NW987 Level 1

      I'm trying to populate an ArrayCollection using a data services call with the following code:

       

       

      [
      Bindable]
      
      public var srv:RemoteObject;
      
      var acCountries:ArrayCollection; 
      
      if
      
      
       (srv == null){srv = 
      new RemoteObject();srv.destination = 
      
      "trip"; }
      acCountries = srv.getAllCountries.lastResult;
      srv.getAllCountries();
      
      
      
      

       

       

       

       

      The arraylist is not being populted on the first call.  The backend java data service method is being called, but the ArrayCollection is not being populated until the second(or third) time the code is excecuted.

       

      I've tried stepping through the debugger, but it's not clear what I'm failing to initialize properly.

        • 1. Re: ArrayCollection Not Populating After First Data Services Call
          SpaghettiCoder Level 3

          you have

           

          acCountries = srv.getAllCountries.lastResult

           

          before

           

          srv.getAllCountries();

           

          it looks like your assigning a result to a arrayCollection, but haven't even called for the data?

          • 2. Re: ArrayCollection Not Populating After First Data Services Call
            NW987 Level 1

            Thanks, but it doesn't seem to matter whether assign the variable or make the data services call first.  In each case, it only works if I execute the code twice.

            • 3. Re: ArrayCollection Not Populating After First Data Services Call
              BhaskerChari Level 4

              Hi,

               

              Try to use the below approach instead of the appraoch you adopted.

               

              [

               

              Bindable]

               

              public var srv:RemoteObject;

               

               

              private function getAllCountries():void

              {

               

               

              var acCountries:ArrayCollection;

               

               

              if(srv == null)

              {

              srv =

              new RemoteObject();

              srv.destination =

              "trip";

              }

              srv.addEventListener(ResultEvent.RESULT, countriesResultHandler);

              srv.addEventListener(FaultEvent.FAULT

               

              , countriesfaultHandler);

              srv.getAllCountries();

              }

               

               

               

              private function countriesResultHandler(result:ResultEvent)

              {

               

              //Here inspect the result object by debugging and replace the below line accordingly

              acCountries = result.countries;

              }

               

               

              private function countriesfaultHandler(fault:FaultEvent)

              {

              Alert.show(

              "Fault:" + fault.message);

              }

               

              In the approach you used you are binding the data to the ArrayCollection by making use of the lastResult object.

               

              But you are using the below line and you are sending the result at the same time .....

               

              acCountries = srv.getAllCountries.lastResult;

              srv.getAllCountries();

               

              Here you don't know when the acCountries arraycollection will bound to the lastResult object . Since it is a asynchronous operation so by the first time you are checking the acCountries arraycollection you are not having any data. Its always better and good appraoch to use response event handlers like the way I used so that you will exactly know when the response/result occured and if any fault or so.

               

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

               

              Hope this will make things clear...

               

               

              Thanks,

              Bhasker Chari

              • 4. Re: ArrayCollection Not Populating After First Data Services Call
                NW987 Level 1

                Bhasker

                 

                Yes, it appears the asynchronous data services call was not populating the arraylist in time.  It is working with the event listener.

                 

                Thanks, Nick.