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:

mySO.data.email="myEmail@mail.com";

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();
mySO.data.products=productsToSave;

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.

Advertisements

5 Responses to “Learning Flex – Lesson 24, Using Shared Objects”

  1. John Doan Says:

    can 2 swf that run on diff browser store and retrieve the same sharedobject. Both swf and html is on the same domain and root?

    • mattreyuk Says:

      Yep – you should be able to. If you look at that Adobe doc referenced in the post under the heading “specifying a path” you’ll see “This is useful if you want more than one application on the client to be able to access the same shared object. In this case, the client could run two Flex applications, both of which specify a path to the shared object that is the root of the domain; the client could then access the same shared object from both applications.”

      I guess that also means the .sol file does not have to be under documents and settings… etc that’s just the default.

  2. John Doan Says:

    Full example would be nice. Where does the .sol file store? I cannot find the file at the location that adobe document specified or entire PC. Thanks.

    • mattreyuk Says:

      Looks like the Adobe docs are a little off (for Windows XP at least) mine are under C:\Documents and Settings\username\Application Data\Macromedia\Flash Player\#SharedObjects (no user_domain level)

  3. John Doan Says:

    I want to store a complex object using SharedObject and registerClassAlias.

    For example, I have a person class that has 3 string fields and a info varialbe of type Info class. The Info class has 2 string fields. I was able to stored and retrieved the information (no page refresh). When refresh the page, I was able to retreive the 3 string fields but unable to retrieve the 2 string fields in the info variable. What’s the problem.
    Thanks.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: