本文主要是介绍HarmonyOS应用五之轻量化数据存储,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、数据存储
/** Copyright (c) 2022 Huawei Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import { promptAction } from '@kit.ArkUI';
import { preferences } from '@kit.ArkData';
import Logger from '../common/utils/Logger';
import CommonConstants from '../common/constants/CommonConstants';
import Fruit from '../viewmodel/Fruit';let context = getContext(this);
let preference: preferences.Preferences;
let preferenceTemp: preferences.Preferences;/*** Preference model.** @param fruitData Fruit data.*/
class PreferenceModel {private fruitData: Fruit = new Fruit();/*** Read the specified Preferences persistence file and load the data into the Preferences instance.*/async getPreferencesFromStorage() {try {preference = await preferences.getPreferences(context, CommonConstants.PREFERENCES_NAME);} catch (err) {Logger.error(CommonConstants.TAG, `Failed to get preferences, Cause: ${err}`);}}/*** Deletes the specified Preferences persistence file from memory and removes the Preferences instance.*/async deletePreferences() {try {await preferences.deletePreferences(context, CommonConstants.PREFERENCES_NAME);} catch(err) {Logger.error(CommonConstants.TAG, `Failed to delete preferences, Cause: ${err}`);};preference = preferenceTemp;this.showToastMessage($r('app.string.delete_success_msg'));}/*** Save the data to the Preferences.** @param fruit Fruit data.*/async putPreference(fruit: Fruit) {if (!preference) {await this.getPreferencesFromStorage();}// The fruit name and fruit quantity data entered by the user are saved to the cached Preference instance.try {await preference.put(CommonConstants.KEY_NAME, JSON.stringify(fruit));} catch (err) {Logger.error(CommonConstants.TAG, `Failed to put value, Cause: ${err}`);}// Store the Preference instance in the preference persistence fileawait preference.flush();}/*** Get preference data.*/async getPreference() {let fruit = '';if (!preference) {await this.getPreferencesFromStorage();}try {// fruit = <string> await preference.get(CommonConstants.KEY_NAME, '');fruit = (await preference.get(CommonConstants.KEY_NAME, '')).toString();} catch (err) {Logger.error(CommonConstants.TAG, `Failed to get value, Cause: ${err}`);}// If the data is empty, a message is displayed indicating that data needs to be written.if (fruit === '') {this.showToastMessage($r('app.string.data_is_null_msg'));return;}this.showToastMessage($r('app.string.read_success_msg'));return JSON.parse(fruit);}/*** Process the data obtained from the database.*/async getFruitData() {this.fruitData = await this.getPreference();return this.fruitData;}/*** Verifies that the data entered is not empty.** @param fruit Fruit data.*/checkFruitData(fruit: Fruit) {if (fruit.fruitName === '' || fruit.fruitNum === '') {this.showToastMessage($r('app.string.fruit_input_null_msg'));return true;}return false;}/*** write data.** @param fruit Fruit data.*/writeData(fruit: Fruit) {// Check whether the data is null.let isDataNull = this.checkFruitData(fruit);if (isDataNull) {return;}// The data is inserted into the preferences database if it is not empty.this.putPreference(fruit);this.showToastMessage($r('app.string.write_success_msg'));}/*** Popup window prompt message.** @param message Prompt message.*/showToastMessage(message: Resource) {promptAction.showToast({message: message,duration: CommonConstants.DURATION});};
}export default new PreferenceModel();
数据存储调用代码:
await preference.put(CommonConstants.KEY_NAME, JSON.stringify(fruit));
2、数据的读取
new ButtonItemData($r('app.string.read_data_btn_text'),() => {// Get data from the preferences database.PreferenceModel.getFruitData().then((resultData: Fruit) => {if (resultData) {this.fruit = resultData;}});}),
async getFruitData() {this.fruitData = await this.getPreference();return this.fruitData;}
async getPreference() {let fruit = '';if (!preference) {await this.getPreferencesFromStorage();}try {// fruit = <string> await preference.get(CommonConstants.KEY_NAME, '');fruit = (await preference.get(CommonConstants.KEY_NAME, '')).toString();} catch (err) {Logger.error(CommonConstants.TAG, `Failed to get value, Cause: ${err}`);}// If the data is empty, a message is displayed indicating that data needs to be written.if (fruit === '') {this.showToastMessage($r('app.string.data_is_null_msg'));return;}this.showToastMessage($r('app.string.read_success_msg'));return JSON.parse(fruit);}
根据debug流程发现请求如下:
PreferenceModel.getFruitData()-》await this.getPreference()(异步)-》await preference.get(CommonConstants.KEY_NAME, ‘’)).toString()(异步)-》然后原路返回PreferenceModel.getFruitData()走完-》this.showToastMessage(判断里面)-》getFruitData()
再执行完await preference.get(CommonConstants.KEY_NAME, ‘’)).toString()(异步)返回时走了then里面已经成功拿到数据了。
3、删除数据
new ButtonItemData($r('app.string.delete_data_file_btn_text'),() => {// Delete database files.PreferenceModel.deletePreferences();// After a database file is deleted, the corresponding data is left blank.this.fruit.fruitName = '';this.fruit.fruitNum = ''})
async deletePreferences() {try {await preferences.deletePreferences(context, CommonConstants.PREFERENCES_NAME);} catch(err) {Logger.error(CommonConstants.TAG, `Failed to delete preferences, Cause: ${err}`);};preference = preferenceTemp;this.showToastMessage($r('app.string.delete_success_msg'));}
await preferences.deletePreferences(context, CommonConstants.PREFERENCES_NAME);是根据实例名称删除指定的实例。
参考链接
这篇关于HarmonyOS应用五之轻量化数据存储的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!