【问题标题】:What is the most efficient way to store and update hierarchical data for a Flex AdvancedDataGrid?为 Flex AdvancedDataGrid 存储和更新分层数据的最有效方法是什么?
【发布时间】:2013-11-05 15:43:58
【问题描述】:
我有一个 Flex 应用程序,它有一个 AdvancedDataGrid,它在树中显示分层数据。我目前将这些数据存储在 XML 中,并且大约每秒更新一次。这在一段时间内运行良好,但我必须存储和更新的数据量最近有所增加,这使应用程序变慢,以至于它变得无法使用并经常崩溃。
所以我需要一种更有效的方式来存储、更新和访问这些数据。主要要求是我用来存储数据的任何东西都需要能够转换为 HierarchicalData 对象,我可以将其用作我的 AdvancedDataGrid 的数据提供者(以启用树显示)。
我正在考虑尝试 ArrayCollection,因为这是 Adobe 提供的示例 (http://livedocs.adobe.com/flex/3/html/help.html?content=advdatagrid_06.html) 中显示的唯一其他类型。这会比使用 XML 更有效吗?是否有另一种数据类型可能比这两种数据类型的访问速度更快?
【问题讨论】:
-
我写下了在处理分层数据here 时发现的一些内容。您可能会发现它很有用。
标签:
xml
apache-flex
hierarchical-data
advanceddatagrid
arraycollection
【解决方案1】:
每当我对数据结构执行许多更新或搜索操作时,我总是将任何串行数据(xml、json 等)转换为强类型模型类。这将允许数据操作尽快执行。 AdvancedDataGrid 使用 HierarchicalData 类来描述结构,因此您只需要确保您的模型符合该结构(或创建自定义描述符)。
例如:
public class MyBranchNode extends EventDispatcher
{
public function MyBranchNode(data:XML, target:IEventDispatcher = null)
{
super(target);
setMemento(data);
}
public var children:Array;
private var _label:String;
[Bindable(event = "labelChange")]
public function get label():String
{
return _label;
}
public function set label(value:String):void
{
if(_label == value)
return;
_label = value;
this.dispatchEvent(new Event("labelChange"));
}
/**
* Parse your XML into into binary data
*/
private function setMemento(data:XML):void
{
this.label = data.label;
for each(var child:XML in data.children())
{
if(this.children == null)
this.children = [];
this.children.push(new MyLeafNode(child));
}
}
}
public class MyLeafNode extends EventDispatcher
{
public function MyLeafNode(data:XML, target:IEventDispatcher = null)
{
super(target);
setMemento(data);
}
private var _label:String;
[Bindable(event = "labelChange")]
public function get label():String
{
return _label;
}
public function set label(value:String):void
{
if(_label == value)
return;
_label = value;
this.dispatchEvent(new Event("labelChange"));
}
/**
* Parse your XML into into binary data
*/
private function setMemento(data:XML):void
{
this.label = data.label;
}
}