0

我现在有两个 ArrayCollection (a, b) 并且 a 设置为可绑定的。我想用b重置a。

我们项目中的旧代码是这样的:

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

然后,我认为这可能会导致潜在的内存泄漏。所以我把它改成:

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

然后我看了一个题目:Flex 优化技巧:ArrayCollection.removeAll() vs. ArrayCollection.source = new Array()。这是一个错误吗? 它说当数据集很大时 removeAll() 会导致性能问题。

那么这是否意味着有一个诡计?如果数据集小我应该使用removeAll,如果数据集很大我不应该使用removeAll()?

另一个问题,我还阅读了有关更改 ArrayCollection 的来源的主题。它说如果直接使用 a = b,“它将杀死所有正在侦听 ArrayCollection 实例上的事件的数据绑定控件”。我不明白这一点。我尝试了 a = b,它工作正常(使用 a 作为数据提供者更新的视图)。

使用 a=b 和 a.source = b.source 有什么区别?

我是 Flex 的新手。提前致谢。

4

2 回答 2

1

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

于 2012-11-14T05:29:57.690 回答
1

我也会尝试:

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

当您声明:

a = new ArrayCollection()

它丢失了指向绑定到应用程序的“旧”ArrayCollection 的指针。因此,这就是为什么当您执行“new ArrayCollection”时绑定不再起作用的原因。但是,在您的示例中,您并没有创建“新的 ArrayCollection”......您只是用其他东西替换了 ArrayCollection 中的对象......所以绑定仍然有效。

如果您有成千上万的数据,您可能需要考虑实现某种分页。如果只是几百个,那么我认为您不必太担心 a.removeAll(); 的性能。

于 2012-11-14T18:21:54.437 回答