> 文档中心 > HarmonyOS应用开发:持久化数据管理

HarmonyOS应用开发:持久化数据管理

方舟开发框架通过PersistentStorage类提供了一些静态方法用来管理应用持久数据,可以将特定标记的持久化数据链接到AppStorage中,并由AppStorage接口访问对应持久化数据,或者通过@StorageLink装饰器来访问对应key的变量。

方法 参数说明 返回值 定义
PersistProp key : string
defaultValue: T
void 关联命名的属性在AppStorage变为持久化数据,赋值覆盖顺序如下:
- 首先,如果该属性存在于AppStorage,将Persistent中的数据复写为AppStorage中的属性值。
- 其次,Persistent中有此命名的属性,使用Persistent中的属性值。
- 最后,以上条件均不满足,则使用defaultValue,不支持null和undefined。
DeleteProp key: string void 取消双向数据绑定,该属性值将从持久存储中删除。
PersistProps keys: {
key: string,
defaultValue: any
}[]
void 关联多个命名的属性绑定。
Keys void Array 返回所有持久化属性的标记。

 说明:

  • PersistProp接口使用时,需要保证输入对应的key应当在AppStorage存在。

  • DeleteProp接口使用时,只能对本次启动已经link过的数据生效。

PersistentStorage.PersistProp("highScore", "0");@Entry@Componentstruct PersistentComponent {    @StorageLink('highScore') highScore: string = '0'    @State currentScore: number = 0    build() { Column() {     if (this.currentScore === Number(this.highScore)) {  Text(`new highScore : ${this.highScore}`)     }     Button() {  Text(`goal!, currentScore : ${this.currentScore}`)      .fontSize(10)     }.onClick(() => {  this.currentScore++  if (this.currentScore > Number(this.highScore)) {      this.highScore = this.currentScore.toString()  }     }) }    }}

因为PersistentStorage和AppStorage的数据是双向绑定的,所以,对PersistentStorage的数据操作其实就是对AppStorage的数据进行操作,用AppStorage的接口来访问AppStorage(PersistentStorage),或者通过@StorageLink装饰器来访问对应key的变量,如

@StorageLink('highScore') highScore: string = '0'

第一个'highScore'是PersistentStorage里的key,第二个'highScore'是AppStorage里的key,一一对应。

51mike麦克疯