Pain with ArrayCollection addItemAt
ArrayCollection has some nifty functionality, but it isn’t always a smooth sailing.
Gotcha 1: addItemAt doesn’t work if the ArrayCollection is sorted. Well, it does make sense, but there are situations when we could take advantage of the fact that sorting doesn’t happen before a refresh() call. Anyway, something that IMHO should be mentioned in the docs rather than only in ListCollectionView source: “if we’re sorted addItemAt is meaningless, just add to the end”.
Gotcha 2: Well, say, that we insist of using our collection, and so we try to remove the sorting to be able to do a meaningfull addItemAt. We set ac.sort=null; but instead of a great success, things explode on addItemAt. Well, the internal logic of ListCollectionView requires you to do a ac.refresh() after setting sort to null; if you don’t do that, it’s internal vars are out of whack and null references creep in. IMHO ArrayCollection should be smart enough to handle removal of sort without a subsequent refresh call.
Gotcha 3: Well, we call the ac.refresh() after setting ac.sort=null but now our item order is all screwed up. Epic phail. So, let’s just give up and copy the collection instead, shall we?
1 | var ac1:ArrayCollection = new ArrayCollection(ac0.toArray()); |
| Print article | This entry was posted by Stepan on June 29, 2010 at 6:50 pm, and is filed under How-tos and Tutorials. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |

about 2 years ago
You should be able to add your new item to the ArrayCollection’s source then call ac.refresh();
about 2 years ago
Hi Glen, yes, I can do that but that will re-sort all the items. What I was whinging about is this scenario:
Admittedly this is something that can be worked around but IMHO ArrayCollection could handle this in a bit more dev-friendly way.