HarmonyOS dataPreferences.Preferences删除失败

调用deletePreference数据并没有删除,重新开启App后依然存在

preferences: dataPreferences.Preferences | null = null;


createDataPreference(context: Context) {
  let options: dataPreferences.Options = { name: 'youxiDataPrefer' };
  this.preferences = dataPreferences.getPreferencesSync(context, options);
}


deletePreference(key: string) {
  if (this.preferences) {
    this.preferences.delete(key, (err, data)=>{
      if (err) {
      }
    })
  }
}


saveDataPreference(key: string, value: string) {
  try {
    this.preferences?.putSync(key, value);
  } catch (err) {
    if (err) {
      console.error("Failed to flush. code =" + err.code + ", message =" + err.message);
    }
  }

  this.preferences?.flush((err: BusinessError) => {
    if (err) {
      console.error("Failed to flush. code =" + err.code + ", message =" + err.message);
      return;
    }
    console.info("Succeeded in flushing.");
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
HarmonyOS
2025-01-09 14:26:50
296浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

请参考以下demo实现:

1、preferenceUtilETS

import preferences from '@ohos.data.preferences';
import { BusinessError } from '@kit.BasicServicesKit';
import { GlobalContext } from "./GlobalContext"
import { common } from '@kit.AbilityKit';

let PREFERENCES_NAME = 'myStore' ;
export class preferenceUtilETS{

  static loadPreference(context:common.UIAbilityContext,name:string){
    let preferenceFun: Function = (() => {
      let preference: Promise<preferences.Preferences> = preferences.getPreferences(context, PREFERENCES_NAME);
      return preference;
    });

    GlobalContext.getContext().setObject('getPreferences', preferenceFun);
  }

  //清空 myStore 库
  static deletePreferences(){
    let context = GlobalContext.getContext().getObject("UIAbilityContext") as common.UIAbilityContext
    preferences.deletePreferences(context, PREFERENCES_NAME);
  }

  static async putPreferenceKeyValue(name:string,key:string,value:preferences.ValueType){
    try{
      let getPreferences: Function = GlobalContext.getContext().getObject('getPreferences') as Function;
      getPreferences().then(async (preferences: preferences.Preferences) => {
        await preferences.put(key,value);
        preferences.flush();
      });
    } catch (err) {
    }
  }

  static async deletePreferenceKey(key:string){
    try{
      let getPreferences: Function = GlobalContext.getContext().getObject('getPreferences') as Function;
      getPreferences().then(async (preferences: preferences.Preferences) => {
        await preferences.delete(key);
        preferences.flush();
      });
    } catch (err) {
    }
  }

  //读取本地数据********************************* MyPreferences
  static async getPreferenceValue(name:string,key:string,defaultValue:preferences.ValueType): Promise<string>{
    try{
      let getPreferences: Function = GlobalContext.getContext().getObject('getPreferences') as Function;
      let str = await (await getPreferences()).get(key,"") as string ;
      return str;
    } catch (err) {
      return "查询失败";
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

2、GlobalContext

export class GlobalContext {
  private constructor() { }
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

3、EntryAbility---onCreate

GlobalContext.getContext().setObject("UIAbilityContext",this.context)
    preferenceUtilETS.loadPreference(this.context,"test")
  • 1.
  • 2.

4、index

Text(`PreferenceUtilString:${this.PreferenceUtilString}`)
  .onClick(async ()=>{
    preferenceUtilETS.putPreferenceKeyValue("test","key1",this.PreferenceUtilString+"value")
    this.PreferenceUtilString = await preferenceUtilETS.getPreferenceValue("test","key1","获取失败") as string
  })

Text(`deletePreferenceKey`)
  .onClick(async ()=>{
    preferenceUtilETS.deletePreferenceKey("key1")
    this.PreferenceUtilString = await preferenceUtilETS.getPreferenceValue("test","key1","获取失败") as string
  })

Text(`clearAll`)
  .onClick(async ()=>{
    preferenceUtilETS.deletePreferences()
    this.PreferenceUtilString = await preferenceUtilETS.getPreferenceValue("test","key1","获取失败") as string
  })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
分享
微博
QQ
微信
回复
2025-01-09 17:01:41


相关问题
dataPreferences.Preferences取不到数据
1254浏览 • 0回复 待解决
如何删除preferences中缓存的数据?
1157浏览 • 1回复 待解决
HarmonyOS dataPreferences存储文件路径
742浏览 • 1回复 待解决
HarmonyOS dataPreferences保存时调用
1002浏览 • 1回复 待解决
HarmonyOS dataPreferences不是持久存储
1315浏览 • 1回复 待解决
preferences:15500000
895浏览 • 1回复 待解决
HarmonyOS @ohos.data.preferences
1318浏览 • 1回复 待解决
Preferences不起作用
4851浏览 • 1回复 待解决
HarmonyOS preferences.putSync 存储报错
1085浏览 • 1回复 待解决
HarmonyOS preferences无法持久化存储
976浏览 • 1回复 待解决