如何在不阻止UI的情况下使用闪存计时器异步轮询文件?

时间:2022-11-10 20:56:22

I have a flex application that repeatedly polls a remote XML file to detect changes, but I've found that once the file hits a certain size, the poll blocks the UI and makes the page unresponsive for a short time.

我有一个flex应用程序,它反复轮询远程XML文件以检测更改,但我发现一旦文件达到一定大小,轮询就会阻止UI并使页面在短时间内无响应。

Is there any way to ensure that the call made to the server or the event from the flash.utils.Timer class runs asynchronously to the main UI thread?

有没有办法确保对服务器的调用或flash.utils.Timer类中的事件异步运行到主UI线程?

3 个解决方案

#1


Branden's right -- the code we write essentially always happens on the main thread; while the network call itself does happen on a background thread, the handling of that call happens on the main one.

Branden的权利 - 我们编写的代码基本上总是发生在主线程上;当网络调用本身确实发生在后台线程上时,该调用的处理发生在主要调用上。

One thing to keep in mind is that the WebService and HTTPService classes will likely attempt to serialize your responses automatically, which could mean taking that processing hit unnecesarily. Using URLLoader, on the other hand, could give you more direct access to the response data, allowing you to work more directly with it without the unnecessary overhead of that built-in processing.

要记住的一件事是WebService和HTTPService类可能会尝试自动序列化您的响应,这可能意味着不必要地处理该处理。另一方面,使用URLLoader可以让您更直接地访问响应数据,从而可以更直接地使用它,而不会产生内置处理的不必要开销。

In that light, if you find you do have to process the entire XML file, you might consider breaking it up into chunks somehow, and distributing the processing of those chunks into separate functions, rather than handling everything within the scope of a single function. Just doing that might allow the player to continue updating the UI while you're processing that big bunch of text (processing a bit, exiting the function, rendering the UI, entering the next function, rendering, and so on); Oliver Goldman, an engineer on the AIR team, did a presentation on this concept at last year's MAX conference.

有鉴于此,如果您发现必须处理整个XML文件,您可能会考虑以某种方式将其分解为块,并将这些块的处理分配到单独的函数中,而不是处理单个函数范围内的所有内容。只是这样做可能允许玩家在处理大量文本(处理一点,退出函数,渲染UI,进入下一个函数,渲染等)时继续更新UI; AIR团队的工程师Oliver Goldman在去年的MAX会议上就这一概念做了演讲。

Hope it helps!

希望能帮助到你!

#2


It sounds like the blocking is caused by Flash parsing the XML rather than the actual loading.

听起来像阻塞是由Flash解析XML而不是实际加载引起的。

If that is the case then you can keep loading the file and just check the size of the raw data you get back - if it's bigger, parse it and take the parsing hit. Otherwise toss the data and wait for the next request.

如果是这种情况,那么你可以继续加载文件,只检查你得到的原始数据的大小 - 如果它更大,解析它并采取解析命中。否则抛出数据并等待下一个请求。

There's no explicit way to do threading with Flash at this time. Certain tasks happen async naturally (network and pixelbender comes to mind) but that's it.

目前没有明确的方法可以使用Flash进行线程化。某些任务自然会发生异步(网络和像素管理器会浮现在脑海中),但就是这样。

#3


Like mentioned, AS3 is single threaded. But there are a couple of ways to handle your situation. Here are ur possible choices:

如上所述,AS3是单线程的。但是有几种方法可以处理你的情况。以下是您可能的选择:

First,make sure you really need to parse entire XML when loaded and cant just keep the loaded xml nodes in memory as the data model (XML being a native data type now). Sometimes I create value objects by passing them an XMLNode and thats kept in memory till a node value is needed at which point I read it. This allows you to keep some of the parsing for later.

首先,确保在加载时确实需要解析整个XML,并且只能将加载的xml节点作为数据模型保存在内存中(XML现在是本机数据类型)。有时我通过传递一个XMLNode来创建值对象,并将其保存在内存中,直到需要一个节点值,然后我才读取它。这允许您稍后保留一些解析。

If you are using an ArrayCollection or similar structure to hold data, try using a primitive Array ( see http://www.arpitonline.com/blog/?p=114 for an issue I ran into)

如果您使用ArrayCollection或类似结构来保存数据,请尝试使用原始数组(有关我遇到的问题,请参阅http://www.arpitonline.com/blog/?p=114)

See if you can creatively use callLater()(http://livedocs.adobe.com/flex/2/langref/mx/core/UIComponent.html#callLater())

看看你是否可以创造性地使用callLater()(http://livedocs.adobe.com/flex/2/langref/mx/core/UIComponent.html#callLater())

Can you pass the data to the client in a native format like SWX or using Remoting

您可以使用SWX等原生格式或使用远程处理将数据传递给客户端

Can you use data paging? ArrayCollections and I am pretty sure XMLCollection support it

你能用数据分页吗? ArrayCollections和我很确定XMLCollection支持它

Side Note:

While AS3 is single threaded, Pixel Bender and I think Alchemy (http://labs.adobe.com/technologies/alchemy/) run on a different thread. There have been a couple of experiments on blogs using Pixel Bender to do calculations which dont slow the UI of the app (Example:http://elromdesign.com/blog/2009/02/09/using-pixel-bender-to-do-heavy-lifting-calculations-makes-flash-player-multi-thread/).

虽然AS3是单线程的,但Pixel Bender和我认为Alchemy(http://labs.adobe.com/technologies/alchemy/)在不同的线程上运行。在使用Pixel Bender进行计算的博客上进行了一些实验,这些实验不会减慢应用程序的UI速度(例如:http://elromdesign.com/blog/2009/02/09/using-pixel-bender-to-做重型起重-计算,使得闪存播放器,多线程/)。

Also please vote on this feature enhancement ticket with Adobe if you feel the need for this feature enough: https://bugs.adobe.com/jira/browse/ASC-3222

如果您觉得需要足够的功能,请在Adobe上使用此功能增强票进行投票:https://bugs.adobe.com/jira/browse/ASC-3222

#1


Branden's right -- the code we write essentially always happens on the main thread; while the network call itself does happen on a background thread, the handling of that call happens on the main one.

Branden的权利 - 我们编写的代码基本上总是发生在主线程上;当网络调用本身确实发生在后台线程上时,该调用的处理发生在主要调用上。

One thing to keep in mind is that the WebService and HTTPService classes will likely attempt to serialize your responses automatically, which could mean taking that processing hit unnecesarily. Using URLLoader, on the other hand, could give you more direct access to the response data, allowing you to work more directly with it without the unnecessary overhead of that built-in processing.

要记住的一件事是WebService和HTTPService类可能会尝试自动序列化您的响应,这可能意味着不必要地处理该处理。另一方面,使用URLLoader可以让您更直接地访问响应数据,从而可以更直接地使用它,而不会产生内置处理的不必要开销。

In that light, if you find you do have to process the entire XML file, you might consider breaking it up into chunks somehow, and distributing the processing of those chunks into separate functions, rather than handling everything within the scope of a single function. Just doing that might allow the player to continue updating the UI while you're processing that big bunch of text (processing a bit, exiting the function, rendering the UI, entering the next function, rendering, and so on); Oliver Goldman, an engineer on the AIR team, did a presentation on this concept at last year's MAX conference.

有鉴于此,如果您发现必须处理整个XML文件,您可能会考虑以某种方式将其分解为块,并将这些块的处理分配到单独的函数中,而不是处理单个函数范围内的所有内容。只是这样做可能允许玩家在处理大量文本(处理一点,退出函数,渲染UI,进入下一个函数,渲染等)时继续更新UI; AIR团队的工程师Oliver Goldman在去年的MAX会议上就这一概念做了演讲。

Hope it helps!

希望能帮助到你!

#2


It sounds like the blocking is caused by Flash parsing the XML rather than the actual loading.

听起来像阻塞是由Flash解析XML而不是实际加载引起的。

If that is the case then you can keep loading the file and just check the size of the raw data you get back - if it's bigger, parse it and take the parsing hit. Otherwise toss the data and wait for the next request.

如果是这种情况,那么你可以继续加载文件,只检查你得到的原始数据的大小 - 如果它更大,解析它并采取解析命中。否则抛出数据并等待下一个请求。

There's no explicit way to do threading with Flash at this time. Certain tasks happen async naturally (network and pixelbender comes to mind) but that's it.

目前没有明确的方法可以使用Flash进行线程化。某些任务自然会发生异步(网络和像素管理器会浮现在脑海中),但就是这样。

#3


Like mentioned, AS3 is single threaded. But there are a couple of ways to handle your situation. Here are ur possible choices:

如上所述,AS3是单线程的。但是有几种方法可以处理你的情况。以下是您可能的选择:

First,make sure you really need to parse entire XML when loaded and cant just keep the loaded xml nodes in memory as the data model (XML being a native data type now). Sometimes I create value objects by passing them an XMLNode and thats kept in memory till a node value is needed at which point I read it. This allows you to keep some of the parsing for later.

首先,确保在加载时确实需要解析整个XML,并且只能将加载的xml节点作为数据模型保存在内存中(XML现在是本机数据类型)。有时我通过传递一个XMLNode来创建值对象,并将其保存在内存中,直到需要一个节点值,然后我才读取它。这允许您稍后保留一些解析。

If you are using an ArrayCollection or similar structure to hold data, try using a primitive Array ( see http://www.arpitonline.com/blog/?p=114 for an issue I ran into)

如果您使用ArrayCollection或类似结构来保存数据,请尝试使用原始数组(有关我遇到的问题,请参阅http://www.arpitonline.com/blog/?p=114)

See if you can creatively use callLater()(http://livedocs.adobe.com/flex/2/langref/mx/core/UIComponent.html#callLater())

看看你是否可以创造性地使用callLater()(http://livedocs.adobe.com/flex/2/langref/mx/core/UIComponent.html#callLater())

Can you pass the data to the client in a native format like SWX or using Remoting

您可以使用SWX等原生格式或使用远程处理将数据传递给客户端

Can you use data paging? ArrayCollections and I am pretty sure XMLCollection support it

你能用数据分页吗? ArrayCollections和我很确定XMLCollection支持它

Side Note:

While AS3 is single threaded, Pixel Bender and I think Alchemy (http://labs.adobe.com/technologies/alchemy/) run on a different thread. There have been a couple of experiments on blogs using Pixel Bender to do calculations which dont slow the UI of the app (Example:http://elromdesign.com/blog/2009/02/09/using-pixel-bender-to-do-heavy-lifting-calculations-makes-flash-player-multi-thread/).

虽然AS3是单线程的,但Pixel Bender和我认为Alchemy(http://labs.adobe.com/technologies/alchemy/)在不同的线程上运行。在使用Pixel Bender进行计算的博客上进行了一些实验,这些实验不会减慢应用程序的UI速度(例如:http://elromdesign.com/blog/2009/02/09/using-pixel-bender-to-做重型起重-计算,使得闪存播放器,多线程/)。

Also please vote on this feature enhancement ticket with Adobe if you feel the need for this feature enough: https://bugs.adobe.com/jira/browse/ASC-3222

如果您觉得需要足够的功能,请在Adobe上使用此功能增强票进行投票:https://bugs.adobe.com/jira/browse/ASC-3222