HarmonyOS 鸿蒙Next PersistentStorage 持久化存储问题ArkTS
HarmonyOS 鸿蒙Next PersistentStorage 持久化存储问题ArkTS ArkUI开发,一个入口Ability,在onCreate方法中初始化持久化数据
PersistentStorage.PersistProps([
{
key: 'token', defaultValue: ''
},
{
key: 'userInfo', defaultValue: '{}'
}
])
这时候在页面中,index.ets使用 AppStorage.Get(‘token’); 获取到的数据是空,AppStorage.SetOrCreate方法后就是app内的全局方法了,即PersistentStorage初始化未成功。
在index.ets中进行PersistentStorage的初始化(同样的代码),在onPageShow()最开始调用就运行正常了。
end:不在Ablility进行app的PersistentStorage初始化操作
当然可能是api9的一个bug
更多关于HarmonyOS 鸿蒙Next PersistentStorage 持久化存储问题ArkTS的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next PersistentStorage 持久化存储问题ArkTS的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS 鸿蒙Next中的PersistentStorage
是用于实现应用数据持久化存储的API,主要用于将应用的数据存储在设备的持久化存储中,以便在应用重启或设备重启后仍然可以访问这些数据。PersistentStorage
通常与AppStorage
结合使用,AppStorage
是应用的内存存储,而PersistentStorage
则是将AppStorage
中的数据持久化到设备存储中。
在ArkTS中,PersistentStorage
的使用方式是通过@PersistentStorage
装饰器来标记需要持久化的属性。例如:
@PersistentStorage
class MyAppStorage {
@PersistProp
public userToken: string = "";
@PersistProp
public settings: any = {};
}
在上述代码中,userToken
和settings
属性被标记为需要持久化存储的属性。当这些属性的值发生变化时,PersistentStorage
会自动将这些变化存储到设备的持久化存储中。
PersistentStorage
的底层实现依赖于HarmonyOS的分布式数据管理框架,能够确保数据在不同设备之间的同步和一致性。它支持多种数据类型,包括基本类型、对象、数组等。
需要注意的是,PersistentStorage
存储的数据是与应用绑定的,即当应用被卸载时,这些数据也会被清除。此外,PersistentStorage
的存储容量有限,开发者需要合理管理存储的数据量,以避免影响应用性能。
总结来说,PersistentStorage
是鸿蒙Next中用于实现应用数据持久化存储的重要API,通过@PersistentStorage
和@PersistProp
装饰器,开发者可以方便地将应用数据存储在设备的持久化存储中。