Harmony数据存储工具类

使用的是mmkv

1、安装mmkv

ohpm install @ohos/mmkv

2、封装

import{MMKV, SerializeBase} from '@ohos/mmkv'

/**
 * 数据存储工具类
 */
class MMKVUtil{
  private filePath:string = ''
  private cachePath:string = ''
  private mmkv:MMKV
  private mmapID:string="MMKV"

  constructor() {
    this.filePath = globalThis.filesDir
    this.cachePath = globalThis.cacheDir
    let backupRootDir=this.filePath+"/mmkv_backup"

    MMKV.initialize(this.filePath+"/mmvk",this.cachePath)
    this.mmkv= MMKV.getBackedUpMMKVWithID(this.mmapID,MMKV.SINGLE_PROCESS_MODE,this.mmapID,backupRootDir)
  }

  /**
   * 存储数据:string
   * @param key
   * @param value
   */
  saveString(key:string,value:string){
    this.mmkv.encode(key,value)
  }

  /**
   * 获取数据:boolean
   * @param key
   * @returns
   */
  getString(ke

猜你喜欢

转载自blog.csdn.net/xiaopihair123/article/details/132622559