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.");
  })
}
HarmonyOS
1天前
浏览
收藏 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 "查询失败";
    }
  }
}

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);
  }
}

3、EntryAbility---onCreate

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

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
  })
分享
微博
QQ
微信
回复
1天前
相关问题
dataPreferences.Preferences取不到数据
495浏览 • 0回复 待解决
如何删除preferences中缓存的数据?
685浏览 • 1回复 待解决
HarmonyOS dataPreferences存储文件路径
276浏览 • 1回复 待解决
HarmonyOS dataPreferences保存时调用
520浏览 • 1回复 待解决
HarmonyOS dataPreferences不是持久存储
717浏览 • 1回复 待解决
HarmonyOS @ohos.data.preferences
708浏览 • 1回复 待解决
preferences:15500000
437浏览 • 1回复 待解决
Preferences不起作用
4323浏览 • 1回复 待解决
HarmonyOS preferences.putSync 存储报错
506浏览 • 1回复 待解决
HarmonyOS preferences无法持久化存储
235浏览 • 1回复 待解决