flex 3:任何人都可以帮我优化这个数组 ->数组集合函数?

发布于 2024-11-04 20:15:31 字数 1260 浏览 4 评论 0原文

我使用父级将多维数组传递给子级。名为 projectPositions 的数组的结构如下(带有示例数据):

projectPositions[0][0] = 1;
projectPositions[0][1] = 5;
projectPositions[0][2] = '1AD';
projectPositions[0][3] = 'User name';

我需要获取这个继承的数组并将其转换为 arrayCollection,以便我可以将其用作 dataProvider。目前,我的 init 函数(在CreationComplete 上运行)包含此代码来处理数组的此任务 -> arrayCollection:

for (var i:int = 0; i < projectPositions.length; i++)
{
tempObject = new Object;
tempObject.startOffset = projectPositions[i][0];
tempObject.numDays = projectPositions[i][1];
tempObject.role = projectPositions[i][2];
tempObject.student = projectPositions[i][3];
positionsAC.addItemAt(tempObject, positionsAC.length);
}

然后,在中继器期间,我使用 PositionAC 作为数据提供程序,并按以下方式引用项目:

<mx:Repeater id="indPositions" dataProvider="{positionsAC}" startingIndex="0" count="{projectPositions.length}">
    <components:block id="thisBlock" offSet="{indPositions.currentItem.startOffset}" numDays="{indPositions.currentItem.numDays}" position="{indPositions.currentItem.role}" sName="{indPositions.currentItem.student}" />
</mx:Repeater>

这一切都工作正常并返回所需的效果,但该应用程序的加载时间约为 10 秒。我 99% 确定加载时间是由数组引起的 ->数组集合 for 循环。有没有更简单的方法来达到预期的效果,而不必等待页面加载那么长时间?

I'm using a parent to pass a multi-dimensional array to a child. Structure of the array, named projectPositions is as follows (with example data):

projectPositions[0][0] = 1;
projectPositions[0][1] = 5;
projectPositions[0][2] = '1AD';
projectPositions[0][3] = 'User name';

I need to take this inherited array and turn it into an arrayCollection so that I can use it as a dataProvider. Currently, my init function (which runs onCreationComplete) has this code in it to handle this task of array -> arrayCollection:

for (var i:int = 0; i < projectPositions.length; i++)
{
tempObject = new Object;
tempObject.startOffset = projectPositions[i][0];
tempObject.numDays = projectPositions[i][1];
tempObject.role = projectPositions[i][2];
tempObject.student = projectPositions[i][3];
positionsAC.addItemAt(tempObject, positionsAC.length);
}

Then, during a repeater, I use positionsAC as the dataprovider and reference the items in the following way:

<mx:Repeater id="indPositions" dataProvider="{positionsAC}" startingIndex="0" count="{projectPositions.length}">
    <components:block id="thisBlock" offSet="{indPositions.currentItem.startOffset}" numDays="{indPositions.currentItem.numDays}" position="{indPositions.currentItem.role}" sName="{indPositions.currentItem.student}" />
</mx:Repeater>

This all works fine and returns the desired effect, but the load time of this application is around 10 seconds. I'm 99% sure that the load time is caused by the array -> arrayCollection for loop. Is there an easier way to achieve the desired effect without having to wait so long for the page to load?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-11-11 20:15:31

您加载项目的问题可能是因为您使用的是转发器而不是列表类。

使用转发器,将在内存中创建一个块,并在屏幕上绘制。因此,如果数组中有 100 个项目,则将创建 100 个块。这可能会减慢初始创建和整个应用程序的速度。

基于列表的类侧重于一种称为渲染器回收的技术;这意味着仅在屏幕上创建和渲染显示的元素。因此,根据设置,无论数组中有多少项,屏幕上通常都会有 7-10 个“块”实例。

The issue your having loading items could be because you are using a repeater instead of a list class.

With a repeater, there will be a block created in memory, and drawn on the screen. So, if you have 100 items in your array, then 100 blocks will be created. this could slow down both initial creation and the overall app.

A list based class focuses on a technique called renderer recycling; which means only the displayed elements are created and rendered on the screen. So, depending on settings, you'd usually have 7-10 'block' instances on the screen, no matter how many items you have in your array.

贱人配狗天长地久 2024-11-11 20:15:31

addItemAt 的更改

positionsAC.addItemAt(tempObject, positionsAC.length);

positionsAC.addItem(tempObject);

会导致集合重新索引,这会大大减慢集合速度。

[编辑]
将此跟踪语句放在循环之前和之后
获取输出并从另一个中减去一个,这将显示循环运行了多少毫秒。

var date:Date = new Date( );
trace( date.getTime())

change

positionsAC.addItemAt(tempObject, positionsAC.length);

to

positionsAC.addItem(tempObject);

addItemAt is causing a reindex of the collection which can greatly slow down the collection.

[EDIT]
Put this trace statement before and after the loop
take the output and subtract one from the other and that will show how many milliseconds the loop has run.

var date:Date = new Date( );
trace( date.getTime())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文