Using Persistent Store via SharedObject

by Retired on ‎03-24-2011 02:17 PM - edited on ‎10-26-2011 03:54 PM by Retired (4,546 Views)

Shared Objects are used to store limited amounts of data on the device. It is smiliar to a browser cookie with two exceptions that it can store more complex data than simple text, and they do not expire by default.


Shared Objects have three important methods:


  • getLocal()Smiley FrustratedharedObject - Used to create a locally persistant shared object or returns a reference if hasn't been previously created.
  • flush()Smiley Frustratedtring - Immediately writes a locally persistent object to a local file.
  • clear():void - Destroys a shared object.

By default, <appname>.sol file which contains your SharedObject is stored within the application directory's data folder "<appname>/data/#SharedObjects/<appname>.swf/" when the flush() function is called.


Due to the BlackBerry® Tablet OS security model, an application can only play within its own sandbox and hence it's SharedObjects can't be accessed by other applications.


The following snippets of code illustrate the use of the SharedObjects:


// Creates the SharedObject variable. The SharedObject is created in
// its default location <appname>/data/#SharedObjects/<appname>.swf/

public var;
sharedObj ="myJournal");


// Adding an Array List to the Object and saving the data = sObjArray;


// Deleting the content of the SharedObject.


 Using an alternate url for your SharedObject:


// Creates the SharedObject variable. The SharedObject is created in
// the set location <appname>/data/#SharedObjects/

public var;
sharedObj ="myJournal", "/");


See the following learning resources for more information on the API used in this article:


 SharedObject API


 A journal sample application is attached demonstrating the use of SharedObjects.