4 Replies Latest reply on Oct 1, 2006 7:38 AM by D._Andy

    Updating ArrayCollections from XML Files

    D._Andy
      I am developing a game and I have a grid where I display all the users with their current states(wins, losses and balance). I retrieve the states of the players from an XML file every 10 seconds. The XML file does not retrun the states of all the players every time I call it, it only returns information about the players that have changes in their states (won or lost).

      The problem is that when I upload the file every 10 seconds all the data is lost an only the uploaded data remains. I have beed searching for a way to only update the (ArrayCollection) that I am using but with no Luck.

      the following is a snapshot of some of the basic code that I am using

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      width="350" height="550"
      creationComplete="uSend();"
      >
      <mx:Script>

      <![CDATA[
      import mx.rpc.events.ResultEvent;
      import mx.collections.ArrayCollection;

      [Bindable]
      private var usersCol:ArrayCollection;

      var load_Interval:uint;

      private function uResultHandler(event:ResultEvent):void {
      usersCol = event.result.users.user;
      }

      function uSend():void {
      clearInterval(load_Interval);
      u.send();
      load_Interval = setInterval(uSend, 10*1000);
      }


      ]]>

      </mx:Script>

      <!-- Service to load in XML -->
      <mx:HTTPService
      id="u"
      url="gamers.xml"
      result="uResultHandler(event);" />

      <mx:Panel title="Top Gamers" width="100%">
      <mx:DataGrid
      id="usersGrid" width="100%" rowCount="20" dataProvider="{usersCol}">
      <mx:columns>
      <mx:DataGridColumn dataField="name" headerText="Name"/>
      <mx:DataGridColumn dataField="loss" headerText="Games Lost"/>
      <mx:DataGridColumn dataField="won" headerText="Games Won"/>
      <mx:DataGridColumn dataField="totalCash" headerText="Total Cash"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>
      </mx:Application>

      and the following is the games.xml file format:

      <users>
      <user>
      <name>player 3</name>
      <loss>1</loss>
      <won>9</won>
      <totalCash>6,825</totalCash>
      </user>
      <user>
      <name>Player 10</name>
      <loss>12</loss>
      <won>7</won>
      <totalCash>2,075</totalCash>
      </user>
      </users>


      What is the best method to resolve this issue without performing so much array search?

      Many thanks

        • 1. Re: Updating ArrayCollections from XML Files
          peterent Level 2
          I think your application is a candidate for using a cursor. An IViewCursor that is. You create a cursor on your collection and in the result handler, use the data coming in to match records in the usersCol collection and update it.

          Right now you are wholesale replacing the collection. You want to go through the new data and match keys (eg, the name field) and replace just those items.

          Read up on IViewCursor - this is what Collections were made for.
          • 2. Re: Updating ArrayCollections from XML Files
            ntsiii Level 3
            I do not know any magical way to update the elements of an array.

            If you use e4x XML as the resultFormat of the data call, you can use the selection expressions to locate the data you need without looping over the entire list.

            Tracy
            • 3. Updating ArrayCollections from XML Files
              D._Andy Level 1
              Thanks for the Advice. I have tried both ways using an IViewCursor and e4x XML resultFormat, and I found the IViewCursor More Convient.

              The following is the code the code that I came up with

              private var userCursor:IViewCursor;
              private var tempCol:ArrayCollection;

              var ap_initialized:Boolean = false;

              private function bsResultHandler(event:ResultEvent):void {

              tempCol = event.result.users.user;
              // Initialization for the first time
              if (!ap_initialized){
              usersCol = tempCol;
              var sort:Sort = new Sort();
              sort.fields = [new SortField("name", true)];
              usersCol.sort = sort;
              usersCol.refresh();
              ap_initialized = true;
              }
              // Disable Auto Update till all the changes are made to the ArrayCollection
              usersCol.disableAutoUpdate();

              userCursor = usersCol.createCursor();
              for (var i=0; i < tempCol.length; i++) {
              //Check if the item already exists
              if (userCursor.findAny({name:tempCol .name})){
              var tmpIndex = usersCol.getItemIndex(userCursor.current);
              usersCol.setItemAt(tempCol[ i ],tmpIndex);
              } else {
              // New Item to be Added
              usersCol.addItem(tempCol[ i ]);
              }

              } // End For

              usersCol.enableAutoUpdate();
              load_Interval = setInterval(uSend, 2*1000);
              }


              I don't think the above the optimal solution. Furthermore, there is a problem with grid; if you have 15 items in the grid, and you scroll to the middle of the grid, it keeps scrolling downwards with each new update. Something really wierd !!!
              • 4. Re: Updating ArrayCollections from XML Files
                D._Andy Level 1
                Can some one help with improving the above logic of updating new items with evey new load of the XML data?!