0

我对 flex 很陌生,不知道发生了什么,哈哈。我想从 xml 文件创建一个大型数据库,但是在将记录插入数据库时​​,应用程序会冻结,直到一切都完成。我有一个创建数据库的 AS3 文件和实际视图中的繁忙指示器,其中按下按钮创建数据库。当我这样做时,代码运行时不显示忙碌指示符。

我在想也许繁忙的指示器应该在实际的 AS3 文件中,但我不知道该怎么做。任何人都可以帮助指出正确的方向吗?谢谢你。

enter code here
<fx:Script>
<![CDATA[
import skins.BusyPopUpSkin;
import spark.components.SkinnablePopUpContainer;
private var mPopUp:SkinnablePopUpContainer;
import model.DBcreateDAO;


protected function button1_clickHandler(event:MouseEvent):void
{
    // if busy, return
    if (mPopUp)
        return;

    createPopUp();
    startOperation();
}

private function createPopUp():void
{
    // create the SkinnablePopUpContainer
    mPopUp = new SkinnablePopUpContainer();
    // set the styles
    mPopUp.setStyle("skinClass", BusyPopUpSkin);
    mPopUp.setStyle("backgroundColor", 0x000000);
    mPopUp.setStyle("backgroundAlpha", 0.3);

    layoutPopUp();

    // call PopUpManger to open and add
    mPopUp.open(this);

    positionPopUp();
}

private function layoutPopUp():void
{
    // match the popups width, height to the View
    mPopUp.width = width;
    mPopUp.height = height;
}

private function positionPopUp():void
{
    // use the View x, y coords
    var point:Point = new Point(x, y);
    // convert the View x,y to global so this can be laid out in nested Views
    point = parent.localToGlobal(point);
    // set the popup's global x,y coords
    mPopUp.x = point.x;
    mPopUp.y = point.y;
}

private function startOperation():void
{
    trace("The busy indicator should be running while the database is being created.");

    load(); //function for the actionscript file to make database 
}

private function endOperation():void
{
    mPopUp.close();
    mPopUp = null;
}

private function load():void {
            data = srv.create(); //where my function is to create my database in DBcreateDAO.as

    }


    ]]>
</fx:Script>

----添加了我如何创建我的数据库----

enter code hereprivate var _sqlConnection:SQLConnection;

    public function get sqlConnection():SQLConnection
    {
        if (_sqlConnection)
            return _sqlConnection;
        openDatabase(File.documentsDirectory.resolvePath("SongListA.db"));
        return _sqlConnection;

    }

    public function loadDB():Song {
            var sql:String = "SELECT id, number, title, artist, item, product FROM song";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var result:Array = stmt.getResult().data;
        if (result && result.length == 1)
            return processRow(result[0]);
        else
            return null;
    }


    public function create(song:Song):void
    {   
        trace(song.title);      
        var sql:String = 
            "INSERT INTO song (id, number, title, artist, item, product) " +
            "VALUES (?,?,?,?,?,?)";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.parameters[0] = song.id;
        stmt.parameters[1] = song.number;
        stmt.parameters[2] = song.title;
        stmt.parameters[3] = song.artist;
        stmt.parameters[4] = song.item;
        stmt.parameters[5] = song.product;
        stmt.execute();
        song.loaded = true;
    }

    protected function processRow(o:Object):Song
    {

        var song:Song = new Song();
        song.id = o.id;
        song.number = o.number == null ? "" : o.number;
        song.title = o.title == null ? "" : o.title;
        song.artist = o.artist == null ? "" : o.artist;
        song.item = o.item == null ? "" : o.item;
        song.product = o.product == null ? "" : o.product;

        song.loaded = true;
        return song;
    }

    public function openDatabase(file:File):void
    {
        var newDB:Boolean = true;
        if (file.exists)
            newDB = false;

        _sqlConnection = new SQLConnection();
        _sqlConnection.open(file);

        if (newDB)
        {
            startOperation();
            createDatabase();
            populateDatabase();

        }
    }

    protected function createDatabase():void
    {
        trace("Creating the database");
        var sql:String = 
            "CREATE TABLE IF NOT EXISTS song ( "+
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "number TEXT, " +
            "title VTEXT, " +
            "artist TEXT, " +
            "item TEXT, " + 
            "product TEXT) ";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();         
    }

    protected function populateDatabase(event:Event):void
    {
        this.addElement(busyIndicator);
        var file:File = File.applicationDirectory.resolvePath("SongListFile.xml");
        var stream:FileStream = new FileStream();
        stream.open(file, FileMode.READ);
        var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));

        stream.close();
        for each (var emp:XML in xml.song)
        {
            var song:Song = new Song();
            song.id = emp.id;
            song.number = emp.number;
            song.title = emp.title;
            song.artist = emp.artist;
            song.item = emp.item;
            song.product = emp.product;
            create(song);
        }

    }
4

2 回答 2

0

AS3 目前是单线程的(在即将发布的版本中正在进行多线程工作)。这意味着,当您的代码正在处理数据和更新数据库时,不会运行其他代码,包括更新繁忙指示器的视觉效果的代码。

在没有看到与 db 相关的代码(我假设它是 SQLite)的情况下给出建议有点困难,但是,规避这个问题的一种方法是在异步模式下处理你的数据库。

我不知道您是如何处理插入的,但是在 SQLite 中,您可以通过手动控制事务何时开始和结束来大大加快它们的速度。否则,它将默认为每次插入一个事务(这将非常慢并且会使您的显示冻结更长时间)。

于 2012-04-23T20:33:34.540 回答
0

正如@Eduardo 所暗示的那样,确保为 SQLite 调用 openAsynch(...) 方法,并且通常在大型集插入/创建期间将所有这些都包装在 begin()/commit() 事务中。否则,如果您执行每个 sqlstatement,您最终会将所有内容作为单个事务处理,这是挂起线程的绞索。

于 2012-04-24T03:10:26.930 回答