当前位置:   article > 正文

纯血鸿蒙开发入门教程之简单的本地数据存储_鸿蒙本地存储

鸿蒙本地存储

关于鸿蒙开发的本地数据存储有很多种方案,往期文章中分享过数据库的用法。但是不是所有的数据存储都需要用到数据库,简单的数据使用数据库存储只会事倍功半。所以今天和大家分享另一种数据存储方案-用户首选项(Preferences)。

Preferences采用Key-Value的形式将数据缓存在内存中,也可以使用flush接口将数据持久化保存。不过Preferences不宜保存大量数据,否则会占用大量内存。

使用教程:

1、导入Preferences模块

import dataPreferences from '@ohos.data.preferences';
  • 1

2、获取Preferences实例

let preferences: dataPreferences.Preferences | null = null;

try {
      dataPreferences.getPreferences(getContext(), 'mystore', (err, preferences) => {
        if (err) {
          console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
          return;
        }
        console.info('Succeeded in getting preferences.');

      })
 } catch (err) {
      console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3、写入数据

preferences.put('score', '100', (err) => {
     if (err) {
       console.error(`Failed to put the value of 'startup'. Code:${err.code},message:${err.message}`);
       return;
     }
     console.info("Succeeded in putting the value of 'startup'.");
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

要注意,此时虽然put成功了,但是只是将数据缓存在内存中,要将数据持久化保存需要在写入的回调中调用flush接口:

if (preferences !== null) {
        preferences.flush((err) => {
             if (err) {
                  console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);
                  return;
              }
              console.info('Succeeded in flushing.');
         })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

如果你使用模拟器,要在菜单->run->Edit Configurations窗口下勾选Keep Application Data。

图片

4、读取数据

if(preferences.has('score')){
    try {
        preferences.get('score', 'default', (err, val) => {
          if (err) {
            console.error(`Failed to get value of 'startup'. Code:${err.code}, message:${err.message}`);
            return;
          }
        
          console.info(`Succeeded in getting value of 'startup'. val: ${val}.`);
        })
      } catch (err) {
        console.error(`Failed to get value of 'startup'. Code:${err.code}, message:${err.message}`);
      }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

如果大家还没有掌握鸿蒙,现在想要在最短的时间里吃透它,我这边特意整理了《鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程》以及《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

在这里插入图片描述

OpenHarmony APP应用开发教程步骤:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

在这里插入图片描述

《鸿蒙开发学习手册》:

如何快速入门:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.基本概念
2.构建第一个ArkTS应用
3.……

在这里插入图片描述

开发基础知识:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私保护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……

在这里插入图片描述

基于ArkTS 开发:https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……

在这里插入图片描述

鸿蒙生态应用开发白皮书V2.0PDF:https://docs.qq.com/doc/DZVVkRGRUd3pHSnFG

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/981385
推荐阅读
相关标签
  

闽ICP备14008679号