来自HTTPService的ArrayCollection在调用后更新并且不更新视图

时间:2011-08-06 06:08:32

标签: sql flex actionscript-3 air

我创建了一个简单的方法来从HTTPService检索XML输出并从中填充数据库,但是由于Flex的异步特性,函数在仍然检索数据时会通过 - 因此导致函数返回null值。请在下面找到代码;

查看: 声明

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:dao="database.*"
    preinitialize="open_databaseConnection();"
    creationComplete="init();" 
    title="Indexes">
<fx:Declarations>
    <dao:IndexesDAO id="srv"/>
</fx:Declarations>

查看 功能

private function open_databaseConnection() : void {
     indexArrayCollection = new ArrayCollection;
     indexArrayCollection = srv.listIndexes();
}

database.IndexesDAO

package database {

import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.events.Event;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

import models.Index;

import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
import mx.utils.ArrayUtil;
import mx.utils.ObjectProxy;

public class IndexesDAO {

    private static var _sqlConnection:SQLConnection;    
    private var SubSonic:HTTPService = new HTTPService;

    public function get sqlConnection() : SQLConnection {
        if (_sqlConnection) return _sqlConnection;

        var file:File = File.applicationStorageDirectory.resolvePath("db.db");
        var fileExists:Boolean = file.exists;
        _sqlConnection = new SQLConnection();
        _sqlConnection.open(file);

        if (!fileExists) {
            createDatabase();
            get_indexesXML(); //Calls populate database on result event.
        }

        return _sqlConnection;
    }

    protected function createDatabase() : void {
        var sql:String = 
            "CREATE TABLE IF NOT EXISTS indexes ( "+
            "id VARCHAR(200) PRIMARY KEY, " +
            "name VARCHAR(200))";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();         
    }

    protected function get_indexesXML() : void {
        var requestString:String;

        requestString = "/rest/index.xml";

        var requestURL:String = Settings.ServerURL + requestString;

        SubSonic.addEventListener(ResultEvent.RESULT, populateDatabase);

        auth_send(requestURL);
    }

    private function auth_send(requestURL:String): void {   

        SubSonic.url = requestURL;
        SubSonic.headers = {Authorization:"Basic " + Settings.EncryptedCreds()};

        SubSonic.send();
    }

    protected function populateDatabase(evt:ResultEvent) : void {

        var indexArrayCollection:ArrayCollection = new ArrayCollection();

        var length:int;
        var i:int;

        length = (evt.result['server-response'].indexes.index.source.length);

        for (i = 0; i < length; i++) {


            var addArray:ArrayCollection;
            if (evt.result['server-response'].indexes.index[i].artist is ArrayCollection) {
                addArray = evt.result['subsonic-response'].indexes.index[i].artist;
            } else if (evt.result['server-response'].indexes.index[i].artist is ObjectProxy) {
                addArray = new ArrayCollection(ArrayUtil.toArray(evt.result['server-response'].indexes.index[i].artist));
            }

            indexArrayCollection.addAll(addArray);
        }

        var IndexesDAO:IndexesDAO = new IndexesDAO();

        for each (var indexArr:Object in indexArrayCollection) {
            var index:Index = new Index();
            index.id = indexArr.id;
            index.name = indexArr.name;
            IndexesDAO.create(index);
        }           
    }

    public function create(index:Index) : void {
        var sql:String = "INSERT INTO indexes (id, name) VALUES (?,?)";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.parameters[0] = index.id;
        stmt.parameters[1] = index.name;
        stmt.execute();
    }

    public function listIndexes() : ArrayCollection {           
        var sql:String = "SELECT * FROM indexes ORDER BY name";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var result:Array = stmt.getResult().data;
        if (result)
        {
            var list:ArrayCollection = new ArrayCollection();
            for (var i:int=0; i<result.length; i++) {
                list.addItem(result[i]);    
            }
            return list;
        }
        else
        {
            return null;
        }

    }

    public function refreshIndexes() : ArrayCollection {
        var sql:String = "DROP TABLE indexes";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();

        return listIndexes();
    }

}
}

正如您可以看到视图调用 listIndexes();

但它可以通过数据库填充,然后才能从HTTPService检索结果,从而产生null结果。关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

您可能希望在populateDatabase结束时发送一个事件并运行

indexArrayCollection = srv.listIndexes();

参与eventlistener。那么你可以确定结果已经加载了。

private function open_databaseConnection() : void {
     indexArrayCollection = new ArrayCollection;
     srv.addEventListener(MyCustomEvent.DATA_LOADED, onDataLoaded);
}

private function onDataLoaded(evt:MyCustomEvent):void
{
    indexArrayCollection = srv.listIndexes();
}