0 Replies Latest reply on Jun 14, 2007 5:37 PM by rmarp

    Collection iteration wierdness

    rmarp Level 1
      Hey folks - I am having a wierd problem. If I am looping through a collection with a filterFunction applied and am making changes to the property values of the objects in the collection. If the objects in the collection are bindable or otherwise dispatch PropertyChangeEvent's then the collection executes the filterFunction immediately. Normally this would be fine but after the filterFunction runs, the object get moved to the end of the collection. As you can imagine, re-ordering objects while trying to iterate through them produces some strange results. I don't know if this has made sense, but please look at my attached sample application that demonstrates the problem. Can somebody give some insight into why the objects are getting re-ordered?

      [code]
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="init()">

      <mx:Script>
      <![CDATA[
      import mx.utils.ObjectProxy;
      import mx.collections.ArrayCollection;

      private function init():void
      {
      // create new collection
      var collection:ArrayCollection = new ArrayCollection();

      //add some dummy objects and wrap them in an ObjectProxy
      collection.addItem( new ObjectProxy({id:"A", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"B", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"C", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"D", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"E", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"F", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"G", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"H", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"I", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"J", foo:"bar"}) );
      collection.addItem( new ObjectProxy({id:"K", foo:"bar"}) );

      // apply a filter (which always returns true so really
      // shouldn't affect anything)
      collection.filterFunction = filterObjs;
      collection.refresh();

      // do a simple loop through the collection and
      // print out the values
      trace("Printing out collection values...");
      for each (o in collection)
      {
      trace(o.id);
      }

      // do another simple loop through the collection
      // but this time modify one of the property values.
      // notice how the iteration through the collection
      // goes awry!
      trace("Modifying a property on each object...");
      for each (var o:Object in collection)
      {
      // print out which object we're at
      trace(o.id);

      // change a property value
      o.foo = "something";
      }
      }

      private function filterObjs(o:Object):Boolean
      {
      return true;
      }
      ]]>
      </mx:Script>

      </mx:Application>
      [/code]

      Any thoughts on this would be much appreciated.

      Regards,
      Ryan