Persistent storage of BlackBerry development platform-Persistent Store

Source: Internet
Author: User

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:

 
 
  1. publicclassMyDataimplementsPersistable{  
  2.  
  3. privateStringm_userName;  
  4. privateStringm_company;  
  5. privateStringm_title;  
  6. privateintm_age;  
  7.  
  8. publicMyData(){}  
  9.  
  10. publicvoidsetUserName(StringuserName){  
  11. m_userName=userName;  
  12. }  
  13.  
  14. publicStringgetUserName(){  
  15. returnm_userName;  
  16. }  
  17.  
  18. publicvoidsetCompany(Stringcompany){  
  19. m_company=company;  
  20. }  
  21.  
  22. publicStringgetCompany(){  
  23. returnm_company;  
  24. }  
  25.  
  26. publicvoidsetTitle(Stringtitle){  
  27. m_title=title;  
  28. }  
  29. publicStringgetTitle(){  
  30. returnm_title;  
  31. }  
  32. publicvoidsetAge(intage){  
  33. m_age=age;  
  34. }  
  35. publicintgetAge(){  
  36. returnm_age;  
  37. }  
  38. }  
  39.  

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

 
 
  1. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  2.  
  3. publicvoidsaveData(){  
  4.  
  5. if(myData==null){  
  6. myData=newMyData();  
  7. }  
  8.  
  9. myData.setUserName("Protoss");  
  10. myData.setCompany("Blizzard");  
  11. myData.setTitle("Manager");  
  12. myData.setAge(26);  
  13.  
  14. if(dataStore==null){  
  15. dataStore=PersistentStore.getPersistentObject(ID);  
  16. }  
  17.  
  18. synchronized(dataStore){  
  19. if(dataStore.getContents()==null){  
  20. dataStore.setContents(myData);  
  21. dataStore.commit();  
  22. }  
  23. }  
  24. }  
  25.  

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

 
 
  1. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  2.  
  3. publicvoidrecoverDataFromStore(){  
  4.  
  5. if(dataStore==null){  
  6. dataStore=PersistentStore.getPersistentObject(ID);  
  7. }  
  8.  
  9. synchronized(dataStore){  
  10.  
  11. if(dataStore.getContents()!=null){  
  12. myData=(MyData)dataStore.getContents();  
  13. }  
  14. }  
  15.  
  16. if(myData!=null){  
  17. userNameField.setText(myData.getUserName());  
  18. companyField.setText(myData.getCompany());  
  19. titleField.setText(myData.getTitle());  
  20. ageField.setText(myData.getAge()+"");  
  21. }  
  22. }  
  23.  

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:

 
 
  1. importnet.rim.device.api.ui.UiApplication;  
  2. publicclassMyAppextendsUiApplication{  
  3. publicstaticvoidmain(String[]args){  
  4. MyAppapp=newMyApp();  
  5. app.enterEventDispatcher();  
  6. }  
  7.  
  8. publicMyApp(){  
  9. MyScreenmyScreen=newMyScreen();  
  10. pushScreen(myScreen);  
  11. }  
  12. }  
  13.  
  14. importnet.rim.device.api.util.Persistable;  
  15. publicclassMyDataimplementsPersistable{  
  16.  
  17. privateStringm_userName;  
  18. privateStringm_company;  
  19. privateStringm_title;  
  20. privateintm_age;  
  21.  
  22. publicMyData(){}  
  23.  
  24. publicvoidsetUserName(StringuserName){  
  25. m_userName=userName;  
  26. }  
  27.  
  28. publicStringgetUserName(){  
  29. returnm_userName;  
  30. }  
  31.  
  32. publicvoidsetCompany(Stringcompany){  
  33. m_company=company;  
  34. }  
  35.  
  36. publicStringgetCompany(){  
  37. returnm_company;  
  38. }  
  39.  
  40. publicvoidsetTitle(Stringtitle){  
  41. m_title=title;  
  42. }  
  43.  
  44. publicStringgetTitle(){  
  45. returnm_title;  
  46. }  
  47.  
  48. publicvoidsetAge(intage){  
  49. m_age=age;  
  50. }  
  51.  
  52. publicintgetAge(){  
  53. returnm_age;  
  54. }  
  55. }  
  56.  
  57. importnet.rim.device.api.system.PersistentObject;  
  58. importnet.rim.device.api.system.PersistentStore;  
  59. importnet.rim.device.api.ui.Field;  
  60. importnet.rim.device.api.ui.FieldChangeListener;  
  61. importnet.rim.device.api.ui.component.BasicEditField;  
  62. importnet.rim.device.api.ui.component.ButtonField;  
  63. importnet.rim.device.api.ui.component.LabelField;  
  64. importnet.rim.device.api.ui.container.MainScreen;  
  65.  
  66. publicclassMyScreenextendsMainScreenimplementsFieldChangeListener{  
  67.  
  68. publicstaticfinallongID=0x5d8a91784555e0f8L;  
  69.  
  70. privatePersistentObjectdataStore;  
  71.  
  72. privateBasicEditFielduserNameField;  
  73. privateBasicEditFieldcompanyField;  
  74. privateBasicEditFieldtitleField;  
  75. privateBasicEditFieldageField;  
  76. privateButtonFieldsaveButton;  
  77.  
  78. privateMyDatamyData;  
  79.  
  80. publicMyScreen(){  
  81. super();  
  82. setTitle("PersistDataTest");  
  83. initialize();  
  84. addToWindow();  
  85. recoverDataFromStore();  
  86. }  
  87.  
  88. privatevoidinitialize(){  
  89. userNameField=newBasicEditField("UserName:","",20,BasicEditField.FILTER_DEFAULT);  
  90. companyField=newBasicEditField("CompanyName:","",20,BasicEditField.FILTER_DEFAULT);  
  91. titleField=newBasicEditField("Title:","",20,BasicEditField.FILTER_DEFAULT);  
  92. ageField=newBasicEditField("Age:","",20,BasicEditField.FILTER_NUMERIC);  
  93. saveButton=newButtonField("save");  
  94. saveButton.setChangeListener(this);  
  95. }  
  96.  
  97. privatevoidaddToWindow(){  
  98. add(userNameField);  
  99. add(titleField);  
  100. add(companyField);  
  101. add(ageField);  
  102. add(saveButton);  
  103. }  
  104.  
  105. publicvoidsaveData(){  
  106.  
  107. if(myData==null){  
  108. myData=newMyData();  
  109. }  
  110.  
  111. myData.setUserName(userNameField.getText());  
  112. myData.setCompany(companyField.getText());  
  113. myData.setTitle(titleField.getText());  
  114. myData.setAge(Integer.valueOf(ageField.getText()).intValue());  
  115.  
  116. if(dataStore==null){  
  117. dataStore=PersistentStore.getPersistentObject(ID);  
  118. }  
  119.  
  120. synchronized(dataStore){  
  121. if(dataStore.getContents()==null){  
  122. dataStore.setContents(myData);  
  123. dataStore.commit();  
  124. }  
  125. }  
  126. }  
  127.  
  128. publicvoidrecoverDataFromStore(){  
  129.  
  130. if(dataStore==null){  
  131. dataStore=PersistentStore.getPersistentObject(ID);  
  132. }  
  133.  
  134. synchronized(dataStore){  
  135.  
  136. if(dataStore.getContents()!=null){  
  137. myData=(MyData)dataStore.getContents();  
  138. }  
  139. }  
  140.  
  141. if(myData!=null){  
  142. userNameField.setText(myData.getUserName());  
  143. companyField.setText(myData.getCompany());  
  144. titleField.setText(myData.getTitle());  
  145. ageField.setText(myData.getAge()+"");  
  146. }  
  147. }  
  148.  
  149. publicvoidfieldChanged(Fieldfield,intcontext){  
  150. if(field==saveButton){  
  151. saveData();  
  152. }  
  153. }  
  154. }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.