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

    Updating ArrayCollections from XML Files

      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"?>
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      width="350" height="550"

      import mx.rpc.events.ResultEvent;
      import mx.collections.ArrayCollection;

      private var usersCol:ArrayCollection;

      var load_Interval:uint;

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

      function uSend():void {
      load_Interval = setInterval(uSend, 10*1000);



      <!-- Service to load in XML -->
      result="uResultHandler(event);" />

      <mx:Panel title="Top Gamers" width="100%">
      id="usersGrid" width="100%" rowCount="20" dataProvider="{usersCol}">
      <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"/>

      and the following is the games.xml file format:

      <name>player 3</name>
      <name>Player 10</name>

      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.

            • 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;
              ap_initialized = true;
              // Disable Auto Update till all the changes are made to the ArrayCollection

              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

              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?!