This article focuses on PersistentStore, the permanent storage of the Blackberry development platform. It mainly includes three storage methods supported by BlackberrySDK5.0 and the requirements of PersistentStore for data types and objects, I believe that you will have a deep understanding of the concept of persistent storage-PersistentStore on the Blackberry development platform.
Persistent storage of Blackberry development platform-PersistentStore
BlackberrySDK5.0 supports three storage methods:
◆ RMS-RecordStore
◆ PersistentStore-persistentstoreforRIMlet
◆ SQLLiteforRIMlet
RMS storage is based on byte arrays. objects must be serialized before being stored. SQLLite is stored in databases and supports SQL statements. This is a new feature of the 5.0SDK. Blackberry is the most mature and popular storage method is PersistentStore, which can store objects directly. This document describes the PersistentStore method in detail.
Requirements of PersistentStore on data types and objects in BlackBerry Development Platform:
◆ The stored object only contains the basic data types-int, String, long, boolean, byte, and so on.
◆ The stored class must execute the Persistable interface.
◆ PersistentStore stores data in Key-Valuepair mode. The key must be a long value and the value type is Object. The following is an example:
1. Create a storage data class, such as classMyData:
- publicclassMyDataimplementsPersistable{
-
- privateStringm_userName;
- privateStringm_company;
- privateStringm_title;
- privateintm_age;
-
- publicMyData(){}
-
- publicvoidsetUserName(StringuserName){
- m_userName=userName;
- }
-
- publicStringgetUserName(){
- returnm_userName;
- }
-
- publicvoidsetCompany(Stringcompany){
- m_company=company;
- }
-
- publicStringgetCompany(){
- returnm_company;
- }
-
- publicvoidsetTitle(Stringtitle){
- m_title=title;
- }
- publicStringgetTitle(){
- returnm_title;
- }
- publicvoidsetAge(intage){
- m_age=age;
- }
- publicintgetAge(){
- returnm_age;
- }
- }
-
Note that the MyData class executes the Persistable interface, which does not have any function to be executed. Instancevariable of MyData is of the basic type. You can also use this method.
2. Store Data
- publicstaticfinallongID=0x5d8a91784555e0f8L;
-
- publicvoidsaveData(){
-
- if(myData==null){
- myData=newMyData();
- }
-
- myData.setUserName("Protoss");
- myData.setCompany("Blizzard");
- myData.setTitle("Manager");
- myData.setAge(26);
-
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
-
- synchronized(dataStore){
- if(dataStore.getContents()==null){
- dataStore.setContents(myData);
- dataStore.commit();
- }
- }
- }
-
PersistentStore is the storage of key-valuepair and requires an ID corresponding to a PersistentObject. It is used for data access. One ID corresponds to only one PersistentObject. The first step of storage is to obtain PersistentObject from PersistentStore with ID, and then call PersistentObject. setContents (Object) interface is used to fill in the storage Object. In this step, the data has not been saved. You need to call PersistentObject. commit () saves data.
3. Get Data
- publicstaticfinallongID=0x5d8a91784555e0f8L;
-
- publicvoidrecoverDataFromStore(){
-
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
-
- synchronized(dataStore){
-
- if(dataStore.getContents()!=null){
- myData=(MyData)dataStore.getContents();
- }
- }
-
- if(myData!=null){
- userNameField.setText(myData.getUserName());
- companyField.setText(myData.getCompany());
- titleField.setText(myData.getTitle());
- ageField.setText(myData.getAge()+"");
- }
- }
-
The first step is to use the ID to obtain the corresponding Persistent Object PersistentObject. getContents () obtains data objects. The retrieved Object class must be converted to actual types, such as myData = (MyData) dataStore. getContents ().
The following is a complete example code of PersistentStore on the BlackBerry development platform. It can be copied to Eclipse for running:
- importnet.rim.device.api.ui.UiApplication;
- publicclassMyAppextendsUiApplication{
- publicstaticvoidmain(String[]args){
- MyAppapp=newMyApp();
- app.enterEventDispatcher();
- }
-
- publicMyApp(){
- MyScreenmyScreen=newMyScreen();
- pushScreen(myScreen);
- }
- }
-
- importnet.rim.device.api.util.Persistable;
- publicclassMyDataimplementsPersistable{
-
- privateStringm_userName;
- privateStringm_company;
- privateStringm_title;
- privateintm_age;
-
- publicMyData(){}
-
- publicvoidsetUserName(StringuserName){
- m_userName=userName;
- }
-
- publicStringgetUserName(){
- returnm_userName;
- }
-
- publicvoidsetCompany(Stringcompany){
- m_company=company;
- }
-
- publicStringgetCompany(){
- returnm_company;
- }
-
- publicvoidsetTitle(Stringtitle){
- m_title=title;
- }
-
- publicStringgetTitle(){
- returnm_title;
- }
-
- publicvoidsetAge(intage){
- m_age=age;
- }
-
- publicintgetAge(){
- returnm_age;
- }
- }
-
- importnet.rim.device.api.system.PersistentObject;
- importnet.rim.device.api.system.PersistentStore;
- importnet.rim.device.api.ui.Field;
- importnet.rim.device.api.ui.FieldChangeListener;
- importnet.rim.device.api.ui.component.BasicEditField;
- importnet.rim.device.api.ui.component.ButtonField;
- importnet.rim.device.api.ui.component.LabelField;
- importnet.rim.device.api.ui.container.MainScreen;
-
- publicclassMyScreenextendsMainScreenimplementsFieldChangeListener{
-
- publicstaticfinallongID=0x5d8a91784555e0f8L;
-
- privatePersistentObjectdataStore;
-
- privateBasicEditFielduserNameField;
- privateBasicEditFieldcompanyField;
- privateBasicEditFieldtitleField;
- privateBasicEditFieldageField;
- privateButtonFieldsaveButton;
-
- privateMyDatamyData;
-
- publicMyScreen(){
- super();
- setTitle("PersistDataTest");
- initialize();
- addToWindow();
- recoverDataFromStore();
- }
-
- privatevoidinitialize(){
- userNameField=newBasicEditField("UserName:","",20,BasicEditField.FILTER_DEFAULT);
- companyField=newBasicEditField("CompanyName:","",20,BasicEditField.FILTER_DEFAULT);
- titleField=newBasicEditField("Title:","",20,BasicEditField.FILTER_DEFAULT);
- ageField=newBasicEditField("Age:","",20,BasicEditField.FILTER_NUMERIC);
- saveButton=newButtonField("save");
- saveButton.setChangeListener(this);
- }
-
- privatevoidaddToWindow(){
- add(userNameField);
- add(titleField);
- add(companyField);
- add(ageField);
- add(saveButton);
- }
-
- publicvoidsaveData(){
-
- if(myData==null){
- myData=newMyData();
- }
-
- myData.setUserName(userNameField.getText());
- myData.setCompany(companyField.getText());
- myData.setTitle(titleField.getText());
- myData.setAge(Integer.valueOf(ageField.getText()).intValue());
-
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
-
- synchronized(dataStore){
- if(dataStore.getContents()==null){
- dataStore.setContents(myData);
- dataStore.commit();
- }
- }
- }
-
- publicvoidrecoverDataFromStore(){
-
- if(dataStore==null){
- dataStore=PersistentStore.getPersistentObject(ID);
- }
-
- synchronized(dataStore){
-
- if(dataStore.getContents()!=null){
- myData=(MyData)dataStore.getContents();
- }
- }
-
- if(myData!=null){
- userNameField.setText(myData.getUserName());
- companyField.setText(myData.getCompany());
- titleField.setText(myData.getTitle());
- ageField.setText(myData.getAge()+"");
- }
- }
-
- publicvoidfieldChanged(Fieldfield,intcontext){
- if(field==saveButton){
- saveData();
- }
- }
- }