Main

February 12, 2008

Data storage and caching with SQLite databases and Adobe AIR

You may or may not know by now that the AIR runtime includes a version of SQLite engine. Being a smaller implementation of SQL, SQLite supports all your usual database transactions, a lot of the complex queries, and triggers. With it,you can create a database to store all the data for your Flex/AIR desktop applications, store data for offline use of your Flex/AIR desktop/web applications, or for caching of data. It's all done in the same way. I was recently working on an application where I wanted the user to have an option to be logged in automatically when the app was launched, so the first time the user logged in I created a database to cache that login info. From then on when the application is launched it checks to see if the database exists, and if so it grabs the login info, and logs them in automatically.

I've made a simple example which is a super simple desktop application which stores it's data in a SQLite database. The idea and implementation here is very similar to what I just described for the caching example. On launch, we first check to see if the database exists. If it does, that means it ought to contain some data so we grab the data and display it. If the database doesn't exist, we create it and add our one default entry, then load it into the application. The user can then add, remove, and update entries. When each of these transactions sends a result of success, we reload all the data in the database. Obviously in a real-world application this wouldn't be a great idea; that's too much overhead. One option would be to just manipulate the dataProvider ArrayCollection after each successful transaction -- but for this simple example I'm leaving it the way it is for the intent of simply demonstrating using SQLite.

At this point I think I'll let the code speak for itself.


ᅠ Basically you'll notice the basic steps are:

1. Create a connection: connection = new SQLConnection();
2. Define the database file: dbFile = File.applicationStorageDirectory.resolvePath(dbFileString);
3. Open (or create and open ) the database: connection.open(dbFile);
4. Create an empty SQLStatement: var sql : SQLStatement = new SQLStatement();
5. Create a query: var sqlString : String = "CREATE TABLE Users (" +
     " uid INTEGER PRIMARY KEY AUTOINCREMENT, " +
     " name TEXT, " +
     " phonenumber TEXT)";
6. Attach the connection and the query to the SQLStatement:
     sql.sqlConnection = connection;
     sql.text = sqlString;
7. Create event listeners for success and failure:
     sql.addEventListener(SQLEvent.RESULT, onDBCreateTableResult);
     sql.addEventListener(SQLErrorEvent.ERROR, onDBCreateTableError);
8. Execute! : sql.execute()



January 21, 2008

InsideRIA launched today

Check out InsideRIA.com which was launched today. It's an online community developed by O'Reilly and sponsored by Adobe, which will provide a number of resources for information about RIA's, RIA development, and that which is related, with lots of attention on Flex, AIR, Actionscript and Flash.

January 14, 2008

Deferred instantiation of mediators in a PureMVC Flex application

I'm creating my first project using PureMVC after using Cairngorm for a few projects. Once I started understanding the concepts and intent behind each of the architecture components (Proxies and Models, Views and Mediators, Controllers and Commands), it's been great in it's relative simplicity. I did printed out the docs and read over them everyday at lunch for a few days in a row just make it all really sink it, but it didn't take too long to click.
The best part is that I've had a few questions so I decided to check out the PureMVC forums on the PureMVC site. So far everything I've searched has returned a thread with a very detailed, extensive answer from Cliff! The days go so much easier when there's easy to find and good information about whatever your working with.

One of the problems I ran into pretty quickly was that I had a TabNavigator with 3 tabs where the contents aren't to be drawn or loaded until they are clicked on.
I initially tried to register the mediators for those 3 view components at startup, which to no surprise threw runtime errors because the views didn't yet exist. Not being sure what the best solution was here, I searched the forums and found this thread.

I went with Cliff's recommendation (deferred instantiation) and here's what I have now:

My TabNavigator has 3 tabs each containing one custom component named "tags", "groups", "info".
In each of these components I have a creationComplete event which calls an onCreationComplete function, which in turn dispatches a custom Event:

dispatchEvent( new ComponentLoadedEvent( ComponentLoadedEvent.COMPONENT_LOADED, true, false, this ) );

The mediator of the TabNavigator itself listens for this event and calls handleLoad when it hears it:

mainComponent.addEventListener( ComponentLoadedEvent.COMPONENT_LOADED, handleLoad );

The handleLoad() registers the appropriate mediator for the loaded component:

public function handleLoad( e : Event ):void
{
    var component :Object 	= e["component"].name;
			
    switch(component)
    { 
        case "tags":
            facade.registerMediator( new TagsMediator( mainComponent.tags )  );
            break;
        case "groups":
            facade.registerMediator( new GroupsMediator( mainComponent.groups )  );
            break;
        case "info":
            facade.registerMediator( new UploadStatusMediator( mainComponent.info )  );
            break;
    }
}