文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
问题处理与运维开发与测试问题应用框架开发本地数据和文件本地数据库管理如何解决传入错误的上下文对象而导致获取首选项实例失败的问题

如何解决传入错误的上下文对象而导致获取首选项实例失败的问题

问题现象

开发者在使用Preferences(用户首选项)来进行持久化数据操作中会出现15500000报错,如何排查?

背景知识

解决方案

发生用户首选项内部错误,一般是因为获取Preferences实例时,传入的context存在问题。可以在应用初始化的过程当中,设置一个全局的应用上下文对象,在获取Preferences实例时,传入这个全局对象。

  1. 在GlobalContext.ets中定义全局上下文对象:
    收起
    自动换行
    深色代码主题
    复制
    1. import { common } from '@kit.AbilityKit';
    2. export class GlobalContext {
    3. private static context: common.UIAbilityContext;
    4. public static initContext(context: common.UIAbilityContext): void {
    5. GlobalContext.context = context;
    6. }
    7. public static getGlobalContext(): common.UIAbilityContext {
    8. return GlobalContext.context;
    9. }
    10. }
  2. 在EntryAbility.ets中初始化全局Context。
    收起
    自动换行
    深色代码主题
    复制
    1. import { UIAbility } from '@kit.AbilityKit';
    2. import { hilog } from '@kit.PerformanceAnalysisKit';
    3. import { window } from '@kit.ArkUI';
    4. import { GlobalContext } from '../pages/GlobalContext';
    5. const DOMAIN = 0x0000;
    6. export default class EntryAbility extends UIAbility {
    7. onDestroy(): void {
    8. hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
    9. }
    10. onWindowStageCreate(windowStage: window.WindowStage): void {
    11. // Main window is created, set main page for this ability
    12. hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    13. windowStage.loadContent('pages/Index', (err) => {
    14. if (err.code) {
    15. return;
    16. }
    17. GlobalContext.initContext(this.context);
    18. hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
    19. });
    20. }
    21. onWindowStageDestroy(): void {
    22. // Main window is destroyed, release UI related resources
    23. hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
    24. }
    25. onForeground(): void {
    26. // Ability has brought to foreground
    27. hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
    28. }
    29. onBackground(): void {
    30. // Ability has back to background
    31. hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
    32. }
    33. };
  3. 通过定义的全局应用上下文对象,来获取对应的首选项实例,并定义存取方法。
    收起
    自动换行
    深色代码主题
    复制
    1. import { preferences } from '@kit.ArkData';
    2. import { GlobalContext } from './GlobalContext';
    3. export class PreferenceUtil {
    4. public static getPreference() {
    5. let options: preferences.Options = { name: 'myStore' };
    6. let preference = preferences.getPreferencesSync(GlobalContext.getGlobalContext(), options);
    7. return preference;
    8. }
    9. public static put(preference: preferences.Preferences, val: string) {
    10. preference.putSync('myStore', val);
    11. }
    12. public static get(preference: preferences.Preferences) {
    13. return preference.getSync('myStore', 'default');
    14. }
    15. }
  4. 在Index.ets中调用存取方法。
    收起
    自动换行
    深色代码主题
    复制
    1. import { PreferenceUtil } from './PreferenceUtil';
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State message: string = '未获取到时间';
    6. build() {
    7. Column() {
    8. Text(this.message)
    9. .margin({ top: 50 });
    10. Row({ space: 50 }) {
    11. Button('存值')
    12. .onClick(() => {
    13. PreferenceUtil.put(PreferenceUtil.getPreference(), new Date().toString());
    14. });
    15. Button('取值')
    16. .onClick(() => {
    17. this.message = PreferenceUtil.get(PreferenceUtil.getPreference()) as string;
    18. });
    19. }
    20. .margin({ top: 20 });
    21. }
    22. .height('100%')
    23. .width('100%');
    24. }
    25. }

总结

开发者在使用用户首选项来进行持久化数据操作中,出现15500000报错,往往是使用getPreferencesSync()或者getPreferences()获取Preferences实例时,传入的context存在问题,建议获取一个全局的context,在调用方法时使用全局context来确保传入应用上下文的正确性。