Development Guide
Persistent data storage
Create a persistent data store
Each PersistentObject has a unique long key.
1.
Import the following classes:
• net.rim.device.api.system.PersistentObject
• net.rim.device.api.system.PersistentStore
• java.lang.String
• net.rim.device.api.ui.component.Dialog
2.
To create a unique long key, in the BlackBerry® Integrated Development Environment, type a string value.
com.rim.samples.docs.userinfo
3.
Right-click the string and click Convert 'com.rim.samples.docs.userinfo' to long.
4.
Include a comment in your code to indicate the string that you used to generate the unique long key.
5.
To create a persistent data store, create a single static PersistentObject and invoke
PersistentStore.getPersistentObject, using the unique long key as a parameter.
static PersistentObject store;
static {
store = PersistentStore.getPersistentObject( 0xa1a569278238dad2L );
}
Store persistent data
1.
Import the following classes:
• net.rim.device.api.system.PersistentStore
• net.rim.device.api.system.PersistentObject
2.
Invoke setContents() on a PersistentObject. This method replaces existing content with the new content.
3.
To save the new content to the persistent store, invoke commit().
String[] userinfo = {username, password};
synchronized(store) {
store.setContents(userinfo);
store.commit();
}
4.
To use a batch transaction to commit objects to the persistent store, invoke
PersistentStore.getSynchObject(). This method retrieves the persistent store monitor that locks the
object.
46
Persistent data storage