Do I use Array or Arraycollection?
Many developers first getting to Flex quickly find out about the ArrayCollection Class and fall in love.
“Really? It’s like an Array PLUS it has binding and filtering and all sorts of other things? Goodbye Array!”
Next thing you know the array is nothing to the developer except for a property that can be accessed via the ArrayCollection().source property. But there are definite times that you need to make the distinct decision between using an Array and an ArrayCollection.
I think it is easier to look at the cases when you would use an ArrayCollection than to look at the cases when you would not use an ArrayCollection. In my opinion, developers should only use ArrayCollections when the Array will be bound to a view. That’s it, simply enough. Binding an Array to a view? Send it through an ArrayCollection so that if the Array updates or changes the CollectionChange event updates the view.
So when would you use an Array? The rest of the time. There is nothing built into an ArrayCollection that isn’t already part of the Array. Plus, you don’t have to deal with the overhead of the ArrayCollection wrapper if it is unnecessary.
I have seen some applications where I have sorted a DataGrid (bound to an ArrayCollection) and another List has updated unexpectedly. This is because the sorting information is shared between Collections, while an Array’s sorting information is not. To solve this unexpected binding I have even seen some developers use the
facility. THIS IS A BAD DECISION. This is bad both for memory and speed depending on how often you are making this copy. Instead, pass around a regular Array and set a
to your view components.
I am open and would love to see you respond back if you disagree or have another specific example of when to use an ArrayCollection vs an Array.