Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [32]

By Root 2528 0

Choosing between synchronous and asynchronous mode


You have a choice of synchronous or asynchronous mode (fileStream open or openAsync). The former uses the application’s main thread, so the application does not execute any other process until a command is completed. Use trace catch to capture any errors. The latter uses a different thread and runs in the background. You need to set listeners to be notified when the command is in progress or completed. Only one mode can be used at a time.

Asynchronous mode is often preferred for mobile development. Choosing one method over the other is also a function of how large the data set is, as well as if the saved data is needed in the current state of your application and the desired user experience.

The write and read examples shown earlier use a synchronized method. To trace errors, use a try catch statement:

try {

var string:String = fileStream.readUTFBytes(fileStream.bytesAvailable);

fileStream.close();

} catch(error:Error) {

trace(error.message);

}

In this asynchronous example, we read a text file and store it when it is received in its entirety:

import flash.events.Event;

import flash.events.ProgressEvent;

var fileStream:FileStream;

var file:File

= File.applicationStorageDirectory.resolvePath("hello.txt");

if (!file.exists) {

return;

}

fileStream = new FileStream();

fileStream.addEventListener(ProgressEvent.PROGRESS, onProgress);

fileStream.addEventListener(Event.COMPLETE, onComplete);

fileStream.openAsync(file, FileMode.READ);

function onProgress(event:ProgressEvent):void {

trace(fileStream.bytesAvailable);

}

function onComplete(event:Event):void {

fileStream.removeEventListener(ProgressEvent.PROGRESS, onProgress);

fileStream.removeEventListener(Event.COMPLETE, onComplete);

var bytes:uint = fileStream.bytesAvailable;

fileStream.close();

}

Writing data and saving it to temporary files


You can write and save data to a temporary directory or file. The data is saved to the application cache. This method is particularly useful if you have data you want to temporarily save during the life of the application while keeping memory free. You can create a directory or a file:

var tempDirectory:File = File.createTempDirectory();

var tempFile:File = File.createTempFile();

trace(tempDirectory.nativePath, tempDirectory.isDirectory);

trace(tempFile.nativePath, tempFile.isDirectory);

Keep a variable reference to the data while the application is running, and delete the file when the application quits. If you want to make the cache data persistent, save its path using its nativePath property, and save its name using its name property, in a SharedObject or another file in the application storage directory. Finally, another good place to save temporary files is the SD card:

tempFile.deleteFile();

tempDirectory.deleteFile();

Unlike AIR on the desktop, when files are deleted they are removed immediately because the device doesn’t have a trash folder. The following command gives the same result as the ones shown earlier:

tempFile.moveToTrash();

tempDirectory.moveToTrash();

In addition to these features, you can also add data to the end of a file; when updating the file, you can read it and write to it at the same time. Also useful is the ability to copy files and folders and move them to another location. You can use this technique to move some the application assets to the SD card. Note, however, that you should never delete any files that came installed with your application, because that will invalidate it.

Using the SQLite Database


Using the SQLite database system is another solution for saving local persistent data, and it is the preferred solution if your information is somewhat complex, if you want the option to organize it in different ways, or if you want to keep it private.

The AIR runtime contains an SQL database engine to create, organize, retrieve, and manipulate the data, using the open source Structured Query Language Lite (SQLite) database system. It does not use the Android OS SQLite framework.

The SQL classes

Return Main Page Previous Page Next Page

®Online Book Reader