当前位置: 首页 > news >正文

【鸿蒙学习笔记】通过用户首选项实现数据持久化

官方文档:通过用户首选项实现数据持久化

目录标题

  • 使用场景
  • 第1步:源码
  • 第2步:启动模拟器
  • 第3步:启动entry
  • 第6步:操作
  • 样例2

使用场景

  1. Preferences会将该数据缓存在内存中,当用户读取的时候,能够快速从内存中获取数据,当需要持久化时可以使用flush接口将内存中的数据写入持久化文件中。
  2. Preferences会随着存放的数据量越多而导致应用占用的内存越大,因此,Preferences不适合存放过多的数据,也不支持通过配置加密,适用的场景一般为应用保存用户的个性化设置(字体大小,是否开启夜间模式)等。

第1步:源码

import { common } from '@kit.AbilityKit';
import dataPreferences from '@ohos.data.preferences';@Entry
@Component
struct Index {@State changeFontSize: number = 16;// 上下文private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext//1. 获取preferenceprivate preferencesInstance: dataPreferences.Preferences = dataPreferences.getPreferencesSync(this.context, { name: 'myStore' });aboutToAppear(): void {//4. 页面打开后,直接从preference中获取上一次的数据let result = this.preferencesInstance.getSync("fontSizeKey", 16)this.changeFontSize = Number(result)}build() {Column() {Row({ space: 10 }) {Text('当前进度一览').fontSize(this.changeFontSize)}.margin(20)Slider({value: this.changeFontSize,min: 14,max: 22,step: 2,style: SliderStyle.InSet}).showSteps(true).width('75%').onChange(async (value: number) => {this.changeFontSize = value//2. 保存数据this.preferencesInstance.putSync('fontSizeKey', this.changeFontSize);//3. 持久化数据this.preferencesInstance.flush()})}.backgroundColor('#f2f3f5').width('100%').height('100%')}
}

第2步:启动模拟器

在这里插入图片描述

第3步:启动entry

在这里插入图片描述

在这里插入图片描述

第6步:操作

在这里插入图片描述

样例2

import dataPreferences from '@ohos.data.preferences';
import { common } from '@kit.AbilityKit';@Entry
@Component
struct Index_preferences2 {@State message: string = 'Hello World';private context:common.UIAbilityContext = getContext(this) as common.UIAbilityContextprivate preferencesInstance: dataPreferences.Preferences =  dataPreferences.getPreferencesSync(this.context, { name: 'myStore' });aboutToAppear(): void {let result = this.preferencesInstance.getSync("messageKey","默认值1")this.message = String(result)}build() {Row() {Column() {TextInput({text:this.message}).fontSize(20).fontWeight(FontWeight.Bold).onChange((value)=>{this.message = value})Button("保存").onClick(()=>{this.preferencesInstance.putSync('message', this.message);this.preferencesInstance.flush()AlertDialog.show({message:"保存成功"})})Button("读取").onClick(() => {let result = this.preferencesInstance.getSync("messageKey","默认值2")this.message = String(result)//获取到的数据不是String,需要转换一下AlertDialog.show({message:this.message})console.log("test",result)})}.width('100%')}.height('100%')}
}

在这里插入图片描述

http://www.lryc.cn/news/397482.html

相关文章:

  • LabVIEW航空发动机试验器数据监测分析
  • 快速上手:前后端分离开发(Vue+Element+Spring Boot+MyBatis+MySQL)
  • 产品推荐| 长江存储eMMC嵌入式储存 YMTC EC230
  • 【Linux】IP地址与主机名
  • ros2--colcon
  • PyCharm 2023.3.2 关闭时一直显示正在关闭项目
  • VS2022 git拉取/推送代码错误
  • 【Vue】vue3中使用swipe竖直方向上滚动
  • 搭建基于 ChatGPT 的问答系统
  • C++运行时类型识别
  • 在微信上怎么制作一个商城链接
  • 怎么搭建微信商城
  • 【每日一练】python的类.对象.成员.行为.方法传参综合实例(保姆式教学)
  • Windows 如何打开表情符号面板并使用?
  • 编程语言里的双斜杠:深入解析其神秘面纱
  • 【架构-20】死锁
  • Chat2DB:AI引领下的全链路数据库管理新纪元
  • 数据库的学习(5)
  • v-bind指令——03
  • RedisTemplate使用
  • 文献解读-多组学-第十八期|《整合 WES 和 RNA-Seq 数据以进行短变异发现》
  • 科学技术奖 | 畜禽粪污源头减排关键技术推广与种养循环一体化农业实践
  • 【漏洞复现】锐捷校园网自助服务系统 任意文件读取
  • Centos9安装部署及静态ip配置方案
  • 利用Altair One 云平台,轻松实现全球企业产品研发创新与优化
  • 数据库树状查询
  • 【实战场景】@Transactional中使用for update的注意点
  • 好用的声音分析的软件和网站
  • 开发情绪识别人工智能时的道德考量
  • MongoDB:基础语句及练习