Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:39:12+00:00 2026-05-24T10:39:12+00:00

I have created a simple method to retrieve XML output from an HTTPService and

  • 0

I have created a simple method to retrieve XML output from an HTTPService and populate a database from it, but due to the async nature of Flex, the functions go through while its still retrieving data – hence causing the function to return a null value. Please find code below;

View: Declarations

<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>

View Functions

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();
    }

}
}

As you can see the view calls
listIndexes();

But it can go through the database population before it can retrieve the result from HTTPService hence producing a null result. Any ideas on how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:39:13+00:00Added an answer on May 24, 2026 at 10:39 am

    you may want to dispatch an event at the end of populateDatabase and run the

    indexArrayCollection = srv.listIndexes();
    

    part in the eventlistener. then you can be sure that the results are already loaded.

    private function open_databaseConnection() : void {
         indexArrayCollection = new ArrayCollection;
         srv.addEventListener(MyCustomEvent.DATA_LOADED, onDataLoaded);
    }
    
    private function onDataLoaded(evt:MyCustomEvent):void
    {
        indexArrayCollection = srv.listIndexes();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple wcf service which used the WCF Service Library template.
I have created a simple grid of divs by left floating them and an
I have created a simple Asp.Net custom control which automatically combines all the correct
I have a simple GtkStatusBar created with glade,and want to add a simple message
I have a requirement to create a simple database in Access to collect some
I have stfw but I cannot find a simple / standalone way to create
I have created a simple UIViewController , and set a UIWebView as a default
I have created a very simple wpf app with mvvm light. I have rows
I have created a Django app. I have a registration page(simple HTML form) in
i am having a very simple problem. I have created an activity in which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.