Learning Flex – Lesson 24, Using Shared Objects

It’s often useful to be able to store data on the client side such as preferences or a saved cart similar to the way websites do using cookies.

Flex has the SharedObject class to achieve this which improves on cookies by being able to store complex data structures.

Properties assigned to a SharedObject are written to a file as soon as the swf is removed from Flash Player either by changing sites or closing down the browser. It’s also possible to manually force the write in the application.

Shared objects are stored on the end users machine in a location determined by the operating system (you can find out the details here ). They have an .sol extension and by default can each be up to 100kb in size (there’s no limit on their number). The user can use the flash player preferences to change this (right click in Flash window, select settings).

Shared objects cannot contain methods and are not deleted by clearing cookies. Like cookies, shared objects cannot be read from different domains. Note that when testing with Flex Builder, shared objects are limited to the same application as the testing environment opens a local file and does not establish a domain.

The static getLocal() method of the SharedObject class retrieves an existing SharedObject or creates a new one. If you need to write the object to file, you can use the static flush() method. Here’s an example of creating a SharedObject:

var mySO:SharedObject = SharedObject.getLocal("shoppingCart");

This creates a file called shoppingCart.sol on the users machine and the SharedObject mySO to refer to it.

To populate the object with data, you assign your values as properties of the data property of the object. For example, to store an email of myEmail@mail.com you would use:


To store complex data objects, you must first create them so to store an array of products you would use:

mySO.data.products=new Array();

When using the flush() method, you may specify a value for the size of the file in bytes. This can be used to create a file larger than initially required. The reason for this is that for the initial write of the file and any time it needs to grow in size, the user will be prompted for approval. by requesting a file size as large as you expect to get, you prevent extra requests for approval every time the shared object is written to.

Flash Player raises an exception when a flush() fails. It returns SharedObjectFlushStatus.FLUSHED
when it’s successful and SharedObjectFlushStatus.PENDING when the size of the file must increase and the user is being prompted for approval, after which a  netStatus event is dispatched with an information object detailing the success or failure of the request.

When working with a shared object, it’s best to check that the value you are about to use is present before attempting to use it. You can achieve this  as follows:

if (mySO.data.email != undefined){
//use the email address

To empty and remove the shared object, use it’s clear() method.

When using SharedObjects in a swf, make sure your display object is at least 215 pixels wide and at least 138 pixels high or the dialog prompt to increase storage cannot be displayed and the flush() call will fail.