| Flex 2 Developer's Guide > Flex Programming Topics > Using Shared Objects > 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 |
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.
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.
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.
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
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
Comments
jacobdol said on Jul 5, 2007 at 4:48 PM :