数据库创建的Flex移动繁忙指示器

时间:2012-04-23 17:38:54

标签: database flex mobile create-table busyindicator

我很新兴,并且不知道发生了什么,哈哈。我想从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);
        }

    }

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

正如@Eduardo所提到的那样,请确保为SQLite调用openAsynch(...)方法,并且通常在大型set insert / create中将所有这些都包含在begin()/ commit()事务中。否则,如果你执行每个sqlstatement,你最终会将所有内容作为单个事务处理,这是挂起你的线程的套索。