自學(xué)鴻蒙應(yīng)用開發(fā)(22)- 在應(yīng)用本地存儲(chǔ)少量數(shù)據(jù)
鴻蒙系統(tǒng)中的輕量級(jí)偏好數(shù)據(jù)庫,主要用于保存應(yīng)用的一些常用配置。數(shù)據(jù)存儲(chǔ)在本地文件中,同時(shí)也加載在內(nèi)存中的,所以訪問速度更快,效率更高。
先看本文的演示視頻:
首先是實(shí)現(xiàn)基本功能。
public class MainAbilitySlice extends AbilitySlice {static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x12345, "MainAbilitySlice");static final String preferenceFile = "preferences";static final String counterKey = "ClickCounter";int clickCounter = 0;public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);readCounter();Text hello = (Text)findComponentById(ResourceTable.Id_text_hello);hello.setClickedListener(new Component.ClickedListener() {public void onClick(Component component) {clickCounter++;try {ohos.global.resource.ResourceManager resManager = getContext().getResourceManager();String hello_msg = resManager.getElement(ResourceTable.String_hello_message).getString();new ToastDialog(getContext()).setText(hello_msg + "X" + clickCounter).setAlignment(LayoutAlignment.BOTTOM).show();} catch (IOException | NotExistException | WrongTypeException e) {e.printStackTrace();}}});
代碼第5行定義了一個(gè)用來表示點(diǎn)擊次次數(shù)的clickCounter,其初值為0。在【你好,鴻蒙?。。 康狞c(diǎn)擊事件處理代碼的第14行會(huì)對(duì)clickCounter進(jìn)行增量操作,當(dāng)每次點(diǎn)擊加1。
如果只是進(jìn)行到這一步,每次程序重啟之后還是會(huì)從1開始計(jì)數(shù)。為了每次操作的結(jié)果不會(huì)丟失,我們首先準(zhǔn)備下面兩個(gè)函數(shù),分別用來從偏好數(shù)據(jù)庫讀出數(shù)據(jù)和向偏好數(shù)據(jù)庫寫入數(shù)據(jù):
private void readCounter(){DatabaseHelper databaseHelper = new DatabaseHelper(getContext());Preferences preferences = databaseHelper.getPreferences(preferenceFile);clickCounter = preferences.getInt(counterKey, 0);}private void writeCounter(){DatabaseHelper databaseHelper = new DatabaseHelper(getContext());Preferences preferences = databaseHelper.getPreferences(preferenceFile);preferences.putInt(counterKey, clickCounter);}
這兩個(gè)方法使用使用的preferenceFile和conterKey分別用來表示偏好數(shù)據(jù)庫的文件名和輸出數(shù)據(jù)的鍵值,它們已經(jīng)實(shí)現(xiàn)在第一段代碼的第3行和第4行定義。這種做法在程序的規(guī)模變大時(shí)更易于管理,可讀性更好。
方法準(zhǔn)備好之后,我們可以向第一段代碼第10行那樣,在Slice類的onStart方法中調(diào)用readCounter,然后像下面代碼中第3行一樣,在Slice類的onStop方法中調(diào)用writeCounter。
public void onStop() {writeCounter();super.onStop();// 打印一條日志HiLog.info(label, "MainAbilitySlice.onStop!");}
這樣就保證了在程序每次退出時(shí)保存counter值,在每次啟動(dòng)時(shí)再將其讀出。

需要注意的是:輕量級(jí)偏好數(shù)據(jù)庫屬于非關(guān)系型數(shù)據(jù)庫,不宜存儲(chǔ)大量數(shù)據(jù),經(jīng)常用于操作鍵值對(duì)形式數(shù)據(jù)的場(chǎng)景。
參考文檔
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-preference-guidelines-0000000000030083
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
