Creating a persistent store
To create a persistent store, you create at least one PersistentObject.
Each PersistentObject has a unique key of type long.
Create a persistent data store
Each PersistentObject has a unique long key.
- Import the required classes and interfaces.
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import java.lang.String;
import net.rim.device.api.ui.component.Dialog;
- To create a unique long key, in the BlackBerry® Integrated Development Environment, type a string value. For exmaple, com.rim.samples.docs.userinfo
- Right-click the string and click Convert ‘com.rim.samples.docs.userinfo’ to long.
- Include a comment in your code to indicate the string that you used to generate the unique long key.
- 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
- Import the required classes and interfaces.
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
- Invoke setContents() on a PersistentObject. This method replaces the existing content with the new content.
- To save the new content to the persistent store, invoke commit().
String[] userinfo = {username, password};
synchronized(store) {
store.setContents(userinfo);
store.commit();
}
- 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.
- Synchronize on the object.
- Invoke commit() as necessary. If any commit in the batch fails, the entire batch transaction fails.
- To commit a monitor object separately from a batch transaction, invoke forceCommit() while synchronizing the monitor object.
Store an object in a batch transaction
- 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.
- Synchronize on the object.
- Invoke commit() as necessary. If any commit in the batch fails, the entire batch transaction fails.
Was this information helpful? Send us your comments.