View comments | RSS feed

Creating a shared object

To create a SharedObject object, use the SharedObject.getLocal() method, which has the following syntax:

SharedObject.getLocal("objectName" [, pathname]): SharedObject

The following example creates a shared object called mySO:

public var mySO:SharedObject;
mySO = SharedObject.getLocal("preferences");

This creates a file on the client's machine called preferences.sol.

The term local refers to the location of the shared object. In this case, Adobe Flash Player stores the SharedObject file locally in the client's home directory.

When you create a shared object, Flash Player creates a new directory for the application and domain. It also creates an empty *.sol file that stores the SharedObject data. The default location of this file is a subdirectory of the user's home directory. On Windows, this directory is the following by default:

c:/Documents and Settings/username/user_domain/Application Data/Macromedia/Flash Player/web_domain/path_to_application/ApplicationName/objectName.sol

If you request an application named MyApp.mxml on the local host, in the Flex context, and within a subdirectory named /sos, Flash Player stores the *.sol file in the following location on Windows:

c:/Documents and Settings/username/user_domain/Application Data/Macromedia/Flash Player/#localhost/flex/sos/MyApp.mxml.swf/objectName.sol

NOTE

 

If you do not provide a name in the SharedObject.getLocal() method, Flash Player names the file undefined.sol.

Although usually predictable, the location of the SharedObject file can be anywhere that Flash Player has access to within its sandbox and can have any name that Flash Player assigns to it.

By default, Flash can save locally persistent SharedObject objects of up to 100 KB per domain. When the application tries to save data to a shared object that would make it bigger than 100 KB, Flash Player displays the Local Storage dialog box, which lets the user allow or deny local storage for the domain that is requesting access.

Subtopics

Specifying a path
Adding data to a shared object
Creating multiple shared objects

Specifying a path

You can use the optional pathname parameter to specify a location for the SharedObject file. This file must be a subdirectory of that domain's SharedObject directory. For example, if you request an application on the localhost and specify the following:

mySO = SharedObject.getLocal("myObjectFile","/");

Flash Player writes the SharedObject file in the /#localhost directory. 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. To share data between more than application without persistence, you can use the LocalConnection object.

If you specify a directory that does not exist, Flash Player does not create a SharedObject file.

Adding data to a shared object

You add data to a SharedObject's *.sol file using the data property of the SharedObject object. To add new data to the shared object, use the following syntax:

sharedObject_name.data.variable = value;

The following example adds the userName, itemNumbers, and adminPrivileges properties and their values to a SharedObject:

public var currentUserName:String = "Reiner";
public var itemsArray:Array = new Array(101,346,483);
public var currentUserIsAdmin:Boolean = true;
mySO.data.userName = currentUserName;
mySO.data.itemNumbers = itemsArray;
mySO.data.adminPrivileges = currentUserIsAdmin;

After you assign values to the data property, you must instruct Flash Player to write those values to the SharedObject's file. To force Flash Player to write the values to the SharedObject's file, use the SharedObject.flush() method, as follows:

mySO.flush();

If you do not call the SharedObject.flush() method, Flash Player writes the values to the file when the application quits. However, this does not provide the user with an opportunity to increase the available space that Flash Player has to store the data if that data exceeds the default settings. Therefore, it is a good practice to call SharedObject.flush().

You can store typed ActionScript instances in shared objects. You do this by calling the flash.net.registerClassAlias() method to register the class. If you create an instance of your class and store it in the data member of your shared object and later read the object out, you will get a typed instance. By default, the SharedObject objectEncoding property supports AMF3 encoding, and unpacks your stored instance from the SharedObject object; the stored instance retains the same type you specified when you called the registerClassAlias() method.

Creating multiple shared objects

You can create multiple shared objects for the same Flex application. To do this, you assign each of them a different instance name, as the following example shows:

public var mySO:SharedObject = SharedObject.getLocal("preferences");
public var mySO2:SharedObject = SharedObject.getLocal("history");

This creates a preferences.sol file and a history.sol file in the Flex application's local directory.


Flex 2.01

Take a survey


Comments


jacobdol said on Jul 5, 2007 at 4:48 PM :
what's the default location of the SharedObjects on Mac OSX?
smgilson said on Jul 6, 2007 at 10:05 AM :
It should be:

app data/Macromedia/Flash Player/#SharedObjects

For example, /Users/JohnD/Library/Preferences/Macromedia/Flash Player/#SharedObjects

Stephen Gilson
Flex Doc Team
No screen name said on Oct 2, 2007 at 7:39 AM :
Above states SharedObjects are stored in /#localhost directory, but this appears to only be true if the app is run while connected to the net. When an app is run offline, SharedObjects are stored in /localhost. It would be useful if this difference was made clear in the docs. Also, it would be helpful if examples were included to show how applications can access SharedObjects across the two different directories or how to make an online app write a SharedObject that is accessible by an offline app.
JabbyPandaUA said on Jun 2, 2008 at 8:36 AM :
I want to make a clarification for the last comment:

1) The path for the stored shared objects will start with a "#localhost" if HTML page that contains SWF file was served via in the browser from local web-server installed (e.g Apache).

2) The path for the stored shared objects will start with a "localhost" if HTML page that contains SWF file was opened in the browser by using "File" > "Open" menu or/and without using web-server locally to serve HTML related content.

The browser address bar will display in this case something similar to this:
file:///C:/workspace/MyProject/bin/MySWFContainingFile.html.


3) The path for the stored shared objects will start with a "#localWithNet" if the SWF file was opened at the standalone Flash 9 EXE viewer.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/201/html/lsos_087_3.html