我有一个as3应用程序,它利用sqlite数据库来保存指标数据。众所周知,as3是单线程的,所以当我保存到这个数据库时,我正在播放的舞台视频变得浑浊!我听说批量保存数据库数据可以加快这个过程,所以我保存了一个循环:
for (var i:int=0; i < metricsObject.metricsComponentData.length; i++){
switch (metricsObject.metricsComponentData[i].mouseType) {
case MetricsCollator.MOUSE_OVER:
this._query_txt = "INSERT INTO " + this._tablePath[0] + " VALUES (null, " + this._sessionID +
", " + metricsObject.metricsComponentData[i].timeStamp +
", '" + metricsObject.metricsComponentData[i].component + "'" + ")";
break;
case MetricsCollator.MOUSE_OUT:
this._query_txt = "INSERT INTO " + this._tablePath[1] + " VALUES (null, " + this._sessionID +
", " + metricsObject.metricsComponentData[i].timeStamp +
", '" + metricsObject.metricsComponentData[i].component + "'" + ")";
break;
} // end switch
executeQuery(this._query_txt);
} // end loop
我已经研究了伪线程示例,但是它们都需要一个sprite或stage实例,我正在执行这个代码的类,不是一个显示类,我真的不想将一个阶段的实例传递给它上课,好像很脏! ; - )
有人有什么想法吗?
答案 0 :(得分:1)
大多数伪线程示例都使用显示对象和输入框架侦听器,但您也可以使用Timer
,或者如果您的应用程序具有某种类型的更新循环。
以下是使用Timer
:
package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class PseudoThreadExample extends Sprite {
private var _timer:Timer;
public function PseudoThreadExample(){
// create a timer that runs with a low interval
// tweak this to fit your needs!
_timer = new Timer(30, 0);
_timer.addEventListener(TimerEvent.TIMER, handleTick);
}
public function startWorking():void {
// put work to be done in list here
// in your case that will probably mean making an array of SQL-
// statements to run later
_timer.start();
}
public function handleTick(event:TimerEvent):void {
// pop a thing off the list and do it.
// if list is empty, stop the timer
_timer.stop();
}
}
}