Flash Lite 2.x and 3.0 ActionScript Language Reference

SharedObject

Object
    |
    +-SharedObject

public dynamic class SharedObject
extends Object

The Flash Lite version of the SharedObject class allows Flash SWF files to save data to the device when it is closed and load that data from the device when it is played again. Flash Lite shared objects store a set of name-value pairs to the device.

Note: The name "SharedObject" is derived from the Flash SharedObject class. The Flash version of this class allows multiple Flash SWF files to share their saved data. However, the Flash Lite version of the SharedObject class does not support sharing data between different Flash SWF files.

In Flash Lite, a SWF file is considered to be a different version if it was modified from the original version, even if it has the same name. This is different than in Flash Player, where a SWF file is considered to be the same if its URL and name are the same, even if the SWF file was modified. In Flash Lite, two different versions of a SWF file can't access each other's shared objects.

To maintain consistency with the Flash platform, the same ActionScript construct and calling conventions are used for the Flash Lite player.

The following examples describe the potential of using shared objects:

  • A Flash application can be used as a user interface for a service that enables the user to search used car listings. The application connects to a server that provides listings of cars based on the search terms and preferences that the user enters. The Flash application can save the last search the user made and prefill the forms the next time the SWF file is played. To do this, you create a SharedObject instance that stores search parameters each time the user makes a new search. When the SWF file closes, the player saves the data in the shared object to the device. The next time the SWF file plays, the Flash Lite player loads the shared object and prefills the search form with the same search data the user entered the previous time.
  • A Flash application can be used as a user interface for a service that allows users to search for music reviews. The application lets users store information about their favorite albums. The information can be stored on the remote server, but this causes problems if the application cannot connect to the service. Also, retrieving the data from a remote service can be slow and detract from the user experience. Shared objects enable the application to store information about the albums to the device and load it quickly when needed.

Note: Because space is limited on mobile devices, the data is not completely persistent; in some situations, the platform could delete the oldest data from the device.

To create a local shared object, use the following syntax:

var so:shared object = shared object.getLocal("mySharedObject");

Reading and writing data on a handset can be slow. To ensure that data is immediately available when the application requests it from the device, Flash Lite 2.0 requires you to set up a listener. The player invokes the listener when the device has loaded the shared object's data. Methods that access the SharedObject instance returned by the call to getLocal() should wait until the listener is invoked before attempting any operations.

Example

In the following example, a SWF file creates a listener function named Prefs and then creates a shared object. The player calls the loadCompletePrefs function when the data is available.

function loadCompletePrefs (mySO:SharedObject) {
    if (0 == mySO.getSize() )
    { 
    // If the size is 0, we need to initialize the data: 
        mySO.data.name = "Sigismund";
        mySO.data.email = "siggy@macromedia.com";
    }
    else
    { 
        // Trace all the data in mySO:
        trace( "Prefs:" );
        for (var idx in mySO.data) {
        trace( " " + idx +": " + mySO.data[idx] ); 
        } 
    }
}

SharedObject.addListener( "Prefs", loadCompletePrefs );

// We can now create the shared object:
var Prefs:SharedObject = SharedObject.getLocal("Prefs");

When the player has notified the listener that the data is available, the application can use the shared object returned from the call to the getLocal() method in the same way a shared object is used in Flash. The application can add, modify, or remove properties while the content is playing. When the content is unloaded, the shared object might be written to the device; however, to guarantee that the shared object will be written to the device, the application must force a write operation by calling the flush() method.

Flash Lite shared objects are available only to locally stored SWF files. SWF files playing back in a network-enabled browser cannot use Flash Lite shared objects.

The total amount of storage for Flash Lite shared objects per SWF file is limited by the deviceto a predetermined size. You can determine this size by using the SharedObject.getMaxSize() method.

Note: Remote shared objects are not supported in Flash Lite 2.0.

See also

flush (SharedObject.flush method), onStatus (SharedObject.onStatus handler)

Property summary

Modifiers

Property

Description

 

data:Object

The collection of attributes assigned to the data property of the object.

Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)

Event summary

Event

Description

onStatus = function(infoObject:Object) {}

Invoked every time an error, warning, or informational note is posted for a shared object.

Method summary

Modifiers

Signature

Description

static

addListener(objectName:String, notifyFunction:Function) : Void

Creates an event listener that the Flash Lite player invokes when the player has loaded the shared object data from the device.

 

clear() : Void

Purges all the data from the shared object and deletes the shared object from the disk.

 

flush(minDiskSpace:Number) : Object

Writes shared object to a local, persistent file.

static

getLocal(name:String) : SharedObject

Returns a reference to a locally persistent shared object that is available only to the current client.

static

getMaxSize() : Number

Returns the total number of bytes the SWF file can use to store mobile shared objects on the device.

 

getSize() : Number

Gets the current size of the shared object, in bytes.

static

removeListener(objectName:String)

Removes any listeners that were added using the addListener() method.

Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)