使用PersistenceV2持久化UI数据,应用第一次启动正常,更新持久化数据后退出应用,同时关闭后台,二次启动应用时闪退,报jscrash异常,代码崩溃在使用PersistenceV2.globalConnect获取持久化数据的地方,错误日志如下:
Error message:For 'User' key: Error: The type of target 'object' mismatches the type of source 'string'
Stacktrace:
at errorHelper (/usr1/hmos_for_system/src/increment/sourcecode/out/generic_generic_arm_64only/general_all_phone_standard/obj/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/stateMgmt.js:15971:1)
at getValueFromDisk (/usr1/hmos_for_system/src/increment/sourcecode/out/generic_generic_arm_64only/general_all_phone_standard/obj/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/stateMgmt.js:15794:1)
at globalConnect (/usr1/hmos_for_system/src/increment/sourcecode/out/generic_generic_arm_64only/general_all_phone_standard/obj/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/stateMgmt.js:15590:1)
at globalConnect (../../../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsStateManagement.js:114:1)
at Index entry (entry/src/main/ets/pages/main.ets:6:37)
at anonymous entry (entry|entry|1.0.0|src/main/ets/pages/Main.ts:66:26) 问题代码示例参考如下:
import { PersistenceV2, Type } from '@kit.ArkUI'; @Entry @ComponentV2 struct Index { @Local user: User = PersistenceV2.globalConnect({ type: User, defaultCreator: () => new User() })!; build() { Column({ space: 20 }) { Text('获取新数据') .width('25%') .borderRadius(10) .onClick(() => { this.getNewAddressAndName(); }) } .justifyContent(FlexAlign.Center) .height('100%') .width('100%') } private async getNewAddressAndName() { this.user.config!.address = 'www.example.com'; this.user.config!.name = ''; console.info(`user.config!.address: ${this.user.config!.address}`) } } @ObservedV2 export class Config { @Trace public address: Resource | string | undefined; @Trace public name: Resource | string | undefined; } @ObservedV2 export class User { constructor() { const config = new Config(); config.address = $r('app.media.default'); config.name = '华为用户'; this.config = config; } @Type(Config) @Trace public config: Config; }
PersistenceV2:为了增强状态管理框架对持久化存储UI的能力,开发者可以使用PersistenceV2存储持久化的数据。PersistenceV2是应用程序中的可选单例对象。
此对象的作用是持久化存储UI相关的数据,以确保这些属性在应用程序重新启动时的值与应用程序关闭时的值相同。PersistenceV2提供状态变量持久化能力,开发者可以通过connect或者globalConnect绑定同一个key,在状态变量变化和应用冷启动时,实现持久化能力。
config.address = $r('app.media.default');
后续运行过程中又被赋了string类型值:
config.address = 'www.example.com';
将User中的address成员改成string类型,不再使用联合类型,应用运行过程中该成员只用于存储string类型的值。
import { PersistenceV2, Type } from '@kit.ArkUI'; @Entry @ComponentV2 struct Index { @Local user: User = PersistenceV2.globalConnect({ type: User, defaultCreator: () => new User() })!; build() { Column({ space: 20 }) { Text('获取新数据') .width('25%') .borderRadius(10) .onClick(() => { this.getNewAddressAndName(); }); } .justifyContent(FlexAlign.Center) .height('100%') .width('100%'); } private async getNewAddressAndName() { this.user.config!.address = 'www.example.com'; this.user.config!.name = ''; console.info(`user.config!.address: ${this.user.config!.address}`); } } @ObservedV2 export class Config { @Trace public address: string = ''; @Trace public name: string = ''; } @ObservedV2 export class User { constructor() { const config = new Config(); config.address = 'init address'; config.name = 'name test'; this.config = config; } @Type(Config) @Trace public config: Config; }