Posts Tagged SharedObject

Storing Custom AMF files for AIR Apps

In my current AIR project I have a need to store persistent user data across sessions. Because the amount of data is fairly small, but the objects are complex, a SQL Lite database wasn’t ideal. I also couldn’t use standard SharedObjects because of the file size limitations and the fact that crashed our IDE (more on that in a later post).

The solution I came to was to make my own SharedObjects, or rather custom files using the AMF data format. It was actually much easier than I had initially feared. I got my start by reading this article by Ted Patrick.  (Unfortunately, the article is hosted on a Sys-Con property, and I’m not a fan of their tatics.)

Ted’s article is short and sweet and shows simply how to read and write AMF-encoded objects from/to a ByteArray.  Since we can easily save ByteArrays to disk and read them out again later, this is a perfect solution to the storage needs.

var byteArray = new ByteArray();
// the writeObject() method encodes any object into AMF
byteArray.writeObject( { myBool: true, myString:"Hello World" });

//the readObject() method decodes the AMF into a standard AS3 Object
var myObject:Object = byteArray.readObject();

So, this approach works great if you are only using simple Objects made up of Flash Player core types. Things get a little more complicated when you need to save complex custom objects. AMF doesn’t automatically recognize your custom objects. This effects all AMF transport methods, including SharedObject, LocalConnection and Remoting.

In order to inform AMF which classes it should recognize, you need to use the registerClassAlias() method:

import flash.net.registerClassAlias;
import com.webdeely.MyCustomClass;

// pass in the path to the class definition and a
// reference to the actual Class
registerClassAlias("com.webdeely.MyCustomClass", MyCustomClass);

var mcc:MyCustomClass = new MyCustomClass();
mcc.message = "Hello World!";
mcc.favoriteNumber = 12;
mcc.userName = "MiltonB";

// ByteArray will now recognize all of your custom class' properties,
// and encode them to AMF
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject( mcc );

// ...and they can be decoded directly back to your class!
var mcc2:MyCustomClass = byteArray.readObject() as MyCustomClass;

Ok, so now we have the encoding and decoding working properly, but we still need to save the file to disk in order for any of this to be useful. For this we’ll use AIR’s File and FileStream classes:

import flash.filesystem.File;
import flash.filesystem.FileStream;

private function writeFile( bytes:ByteArray ):void
{
  // save our data to the user's desktop, in a file called "test.sol"
  //  btw, 'sol' is not a required extension, in fact you don't need an extension
  var file:File = File.desktopDirectory.resolvePath("test.sol");

  // Create a file stream to write stuff to the file.
  var stream:FileStream = new FileStream();

  // Open the file stream and write the data
  stream.open( file, FileMode.WRITE );
  stream.writeBytes(bytes,0,bytes.bytesAvailable);
  stream.close();
}

private function loadFile():ByteArray
{
  var bytes:ByteArray = new ByteArray();
  var file:File = File.desktopDirectory.resolvePath("test.sol");
  var stream:FileStream = new FileStream();

  // Open the file stream in read mode
  stream.open( file, FileMode.READ );

  // read the bytes directly into the ByteArray
  stream.readBytes(bytes, 0, stream.bytesAvailable);
  stream.close();
  return bytes;
}

Hopefully this will be helpful to anyone faced with similar challenges. I have found the de/serialization of the AMF objects to be extremely fast, reliable and convenient. It works wonders and was a very quick switch over from the standard SharedObject process.

I have put together a simple Flex project file for an AIR app that allows you to experiment with the code here. You can download the package here.

,

7 Comments