0
votes

I have two ArrayCollection now (a, b) and a is set bindable. I want to reset a with b.

The old code in our project is like:

a = new ArrayCollection();
for (var i:int = 0; i < b.length; i++) {
    a.addItem(b.getItemAt(i));
}

Then, I think it may cause a potential memory leak. So I changed it to:

a.removeAll();
for (var i:int = 0; i < b.length; i++) {
    a.addItem(b.getItemAt(i));
}

Then I read a topic: Flex optimization tip: ArrayCollection.removeAll() vs. ArrayCollection.source = new Array(). Is this a bug ? It says removeAll() will cause a performance problem when the data set is large.

So does it means there is a trick off? If the data set is small I should use removeAll, and if the data set is large, I should not use removeAll()?

Another question, I also read a topic about Changing the source of an ArrayCollection. It says if directly use a = b, "it will kill all the databound controls that are listening to events on the ArrayCollection instance". I don't understand this. I tried a = b, and it works ok (the view that use a as dataprovider updates).

What's the difference between using a=b and a.source = b.source?

I'm new to Flex. Thanks in advance.

2
I'm not sure what "I want to reset a with b" means. From the code you want to copy all of the elements from b into a?JeffryHouser

2 Answers

1
votes

ArrayCollection is a wrapper class around Array, and underlying Array can be access using source property

The ArrayCollection class is a wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces. Operations on a ArrayCollection instance modify the data source; for xample, if you use the removeItemAt() method on an ArrayCollection, you remove the item from the underlying Array.

  • so one should always use Source property of ArrayCollection, if have populated Array i suggest to declare b as Array not as ArrayCollection and initialize a as
a = new ArrayCollection(b); or 

a= new ArrayCollection();// Default constructor ArrayCollection(source:Array=null);
a.source = b; //updates data in Arraycollection

Data Binding means bound controls with datasource(could be any thing like functions, objects, other control, Array, XML, Collection, List etc)

Data binding is the process of tying the data in one object to another object. It provides a convenient way to pass data between the different layers of the application. Data binding requires a source property, a destination property, and a triggering event that indicates when to copy the data from the source to the destination. An object dispatches the triggering event when the source property changes.

  • Data Binding could be harmful for application with large data because it would creates multiple changes events and both getter and setter executes on change, which need extra proccessing so it would be good practice to shorter scope of a and provide data directly to source as
private function handler_B_DataChange(event:Event)
{
var a:Arraycollection = new ArrayCollection(b);
controlA.dataProvider = a;
//or just
controlB.dataProvider = new ArrayCollection(b);
}

Details of binding could be view on Binding to functions, Objects, and Arrays

Hopes that Helps

1
votes

I would also try:

a.removeAll();
a.addAll(b.list);

When you declare:

a = new ArrayCollection()

it loses the pointer to the "old" ArrayCollection where it is binded to your application. Thus, that is why when you do "new ArrayCollection" the binding doesn't work anymore. However, in your example, you're not creating a "new ArrayCollection"... you're just replacing the objects in that ArrayCollection with something else... So the binding still works.

If you have data that is into the thousands, you might want to consider implementing a pagination of some sort. If it's just a couple hundred, then I don't think you need to worry too much about the performance of a.removeAll();