当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1.4K Star 7.4K Fork 8.2K

OpenHarmony/docs
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docker
en
zh-cn
application-dev
IDL
ability
connectivity
database
device-usage-statistics
device
dfx
internationalization
media
napi
notification
public_sys-resources
quick-start
reference
apis
figures
public_sys-resources
Readme-CN.md
js-apis-Bundle-InnerBundleManager.md
js-apis-Bundle-distributedBundle.md
js-apis-Bundle.md
js-apis-Context.md
js-apis-DataUriUtils.md
js-apis-WorkSchedulerExtensionAbility.md
js-apis-ability-context.md
js-apis-ability-errorCode.md
js-apis-ability-wantConstant.md
js-apis-abilityAccessCtrl.md
js-apis-abilityDelegatorRegistry.md
js-apis-abilityrunninginfo.md
js-apis-abilitystagecontext.md
js-apis-accessibility.md
js-apis-animator.md
js-apis-appAccount.md
js-apis-application-MissionSnapshot.md
js-apis-application-StartOptions.md
js-apis-application-Want.md
js-apis-application-WindowExtensionAbility.md
js-apis-application-ability.md
js-apis-application-abilityConstant.md
js-apis-application-abilityDelegator.md
js-apis-application-abilityDelegatorArgs.md
js-apis-application-abilityLifecycleCallback.md
js-apis-application-abilityManager.md
js-apis-application-abilityMonitor.md
js-apis-application-abilitystage.md
js-apis-application-applicationContext.md
js-apis-application-context.md
js-apis-application-shellCmdResult.md
js-apis-application-staticSubscriberExtensionAbility.md
js-apis-appmanager.md
js-apis-arraylist.md
js-apis-audio.md
js-apis-backgroundTaskManager.md
js-apis-battery-info.md
js-apis-bluetooth.md
js-apis-brightness.md
js-apis-bundle-AbilityInfo.md
js-apis-bundle-ApplicationInfo.md
js-apis-bundle-BundleInfo.md
js-apis-bundle-BundleInstaller.md
js-apis-bundle-CustomizeData.md
js-apis-bundle-ExtensionAbilityInfo.md
js-apis-bundle-HapModuleInfo.md
js-apis-bundle-Metadata.md
js-apis-bundle-ModuleInfo.md
js-apis-bundle-PermissionDef.md
js-apis-bundle-ShortcutInfo.md
js-apis-bundle-remoteAbilityInfo.md
js-apis-bytrace.md
js-apis-call.md
js-apis-camera.md
js-apis-cardEmulation.md
js-apis-commonEvent.md
js-apis-config-policy.md
js-apis-configuration.md
js-apis-configurationconstant.md
js-apis-connectedTag.md
js-apis-contact.md
js-apis-convertxml.md
js-apis-data-ability.md
js-apis-data-distributedobject.md
js-apis-data-preferences.md
js-apis-data-rdb.md
js-apis-data-resultset.md
js-apis-data-storage.md
js-apis-dataAbilityHelper.md
js-apis-deque.md
js-apis-device-info.md
js-apis-device-manager.md
js-apis-deviceUsageStatistics.md
js-apis-display.md
js-apis-distributed-account.md
js-apis-distributed-data.md
js-apis-document.md
js-apis-emitter.md
js-apis-enterprise-device-manager.md
js-apis-environment.md
js-apis-eventhub.md
js-apis-extension-context.md
js-apis-extensionrunninginfo.md
js-apis-faultLogger.md
js-apis-featureAbility.md
js-apis-fileio.md
js-apis-filemanager.md
js-apis-formInfo.md
js-apis-formbindingdata.md
js-apis-formerror.md
js-apis-formextension.md
js-apis-formextensioncontext.md
js-apis-formhost.md
js-apis-formprovider.md
js-apis-geolocation.md
js-apis-hashmap.md
js-apis-hashset.md
js-apis-hiappevent.md
js-apis-hichecker.md
js-apis-hidebug.md
js-apis-hilog.md
js-apis-hitracechain.md
js-apis-hitracemeter.md
js-apis-http.md
js-apis-huks.md
js-apis-i18n.md
js-apis-image.md
js-apis-inputconsumer.md
js-apis-inputdevice.md
js-apis-inputevent.md
js-apis-inputeventclient.md
js-apis-inputmethod.md
js-apis-inputmethodengine.md
js-apis-inputmonitor.md
js-apis-intl.md
js-apis-keycode.md
js-apis-keyevent.md
js-apis-lightweightmap.md
js-apis-lightweightset.md
js-apis-linkedlist.md
js-apis-list.md
js-apis-logs.md
js-apis-media.md
js-apis-medialibrary.md
js-apis-mediaquery.md
js-apis-missionManager.md
js-apis-mouseevent.md
js-apis-net-connection.md
js-apis-nfcController.md
js-apis-nfcTag.md
js-apis-notification.md
js-apis-observer.md
js-apis-osAccount.md
js-apis-particleAbility.md
js-apis-pasteboard.md
js-apis-permissionrequestresult.md
js-apis-plainarray.md
js-apis-power.md
js-apis-process.md
js-apis-processrunninginfo.md
js-apis-prompt.md
js-apis-queue.md
js-apis-radio.md
js-apis-reminderAgent.md
js-apis-request.md
js-apis-resource-manager.md
js-apis-router.md
js-apis-rpc.md
js-apis-runninglock.md
js-apis-screen-lock.md
js-apis-screen.md
js-apis-screenshot.md
js-apis-securityLabel.md
js-apis-sensor.md
js-apis-service-extension-ability.md
js-apis-service-extension-context.md
js-apis-settings.md
js-apis-sim.md
js-apis-sms.md
js-apis-socket.md
js-apis-stack.md
js-apis-statfs.md
js-apis-storage-statistics.md
js-apis-system-app.md
js-apis-system-battery.md
js-apis-system-bluetooth.md
js-apis-system-brightness.md
js-apis-system-cipher.md
js-apis-system-configuration.md
js-apis-system-device.md
js-apis-system-fetch.md
js-apis-system-file.md
js-apis-system-location.md
js-apis-system-mediaquery.md
js-apis-system-network.md
js-apis-system-notification.md
js-apis-system-package.md
js-apis-system-parameter.md
js-apis-system-prompt.md
js-apis-system-request.md
js-apis-system-router.md
js-apis-system-sensor.md
js-apis-system-storage.md
js-apis-system-time.md
js-apis-system-vibrate.md
js-apis-telephony-data.md
js-apis-testRunner.md
js-apis-thermal.md
js-apis-timer.md
js-apis-touchevent.md
js-apis-treemap.md
js-apis-treeset.md
js-apis-uitest.md
js-apis-update.md
js-apis-uri.md
js-apis-uripermissionmanager.md
js-apis-url.md
js-apis-usb.md
js-apis-useriam-userauth.md
js-apis-util.md
js-apis-vector.md
js-apis-vibrator.md
js-apis-volumemanager.md
js-apis-wallpaper.md
js-apis-wantAgent.md
js-apis-webSocket.md
js-apis-webgl.md
js-apis-webgl2.md
js-apis-wifi.md
js-apis-wifiext.md
js-apis-window.md
js-apis-workScheduler.md
js-apis-worker.md
js-apis-xml.md
js-apis-zlib.md
arkui-js
arkui-ts
native-apis
native-lib
Readme-CN.md
security
task-management
telephony
ui
webgl
windowmanager
Readme-CN.md
application-dev-guide-for-gitee.md
application-dev-guide.md
website.md
contribute
design
device-dev
figures
readme
release-notes
OpenHarmony-Overview_zh.md
glossary.md
readme.md
website.md
.gitattributes
.gitignore
CODEOWNERS
DCO.txt
LICENSE
OAT.xml
README.md
README_zh.md
克隆/下载
js-apis-data-storage.md 23.85 KB
一键复制 编辑 原始数据 按行查看 历史

轻量级存储

轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。

说明:

  • 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

  • 从API Version 9开始,该接口不再维护,推荐使用新接口@ohos.data.preferences

导入模块

import data_storage from '@ohos.data.storage';

常量

系统能力: 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core

名称 参数类型 可读 可写 说明
MAX_KEY_LENGTH string key的最大长度限制,需小于80字节。
MAX_VALUE_LENGTH string value的最大长度限制,需小于8192字节。

data_storage.getStorageSync

getStorageSync(path: string): Storage

读取指定文件,将数据加载到Storage实例,用于数据操作。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

返回值:

类型 说明
Storage 获取到要操作的Storage实例,用于进行数据存储操作。

示例:

import data_storage from '@ohos.data.storage'

let path = '/data/storage/el2/database'
let storage = data_storage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()

data_storage.getStorage

getStorage(path: string, callback: AsyncCallback<Storage>): void

读取指定文件,将数据加载到Storage实例,用于数据操作,使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。
callback AsyncCallback<Storage> 回调函数。

示例:

import data_storage from '@ohos.data.storage'

let path = '/data/storage/el2/database'
data_storage.getStorage(path + '/mystore', function (err, storage) {
    if (err) {
        console.info("Get the storage failed, path: " + path + '/mystore')
        return;
    }
    storage.putSync('startup', 'auto')
    storage.flushSync()
})

data_storage.getStorage

getStorage(path: string): Promise<Storage>

读取指定文件,将数据加载到Storage实例,用于数据操作,使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

返回值:

类型 说明
Promise<Storage> Promise实例,用于异步获取结果。

示例:

import data_storage from '@ohos.data.storage'

let path = '/data/storage/el2/database'

let getPromise = data_storage.getStorage(path + '/mystore')
getPromise.then((storage) => {
    storage.putSync('startup', 'auto')
    storage.flushSync()
}).catch((err) => {
    console.info("Get the storage failed, path: " + path + '/mystore')
})

data_storage.deleteStorageSync

deleteStorageSync(path: string): void

从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

示例:

let path = '/data/storage/el2/database'
data_storage.deleteStorageSync(path + '/mystore')

data_storage.deleteStorage

deleteStorage(path: string, callback: AsyncCallback<void>): void

从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。
callback AsyncCallback<void> 回调函数。

示例:

let path = '/data/storage/el2/database'
data_storage.deleteStorage(path + '/mystore', function (err) {
    if (err) {
        console.info("Deleted failed with err: " + err)
        return
    }
    console.info("Deleted successfully.")
})

data_storage.deleteStorage

deleteStorage(path: string): Promise<void>

从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

返回值:

类型 说明
Promise<void> Promise实例,用于异步获取结果。

示例:

let path = '/data/storage/el2/database'
let promisedelSt = data_storage.deleteStorage(path + '/mystore')
promisedelSt.then(() => {
    console.info("Deleted successfully.")
}).catch((err) => {
    console.info("Deleted failed with err: " + err)
})

data_storage.removeStorageFromCacheSync

removeStorageFromCacheSync(path: string): void

从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

示例:

let path = '/data/storage/el2/database'
data_storage.removeStorageFromCacheSync(path + '/mystore')

data_storage.removeStorageFromCache

removeStorageFromCache(path: string, callback: AsyncCallback<void>): void

从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。
callback AsyncCallback<void> 回调函数。

示例:

let path = '/data/storage/el2/database'
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
    if (err) {
        console.info("Removed storage from cache failed with err: " + err)
        return
    }
    console.info("Removed storage from cache successfully.")
})

data_storage.removeStorageFromCache

removeStorageFromCache(path: string): Promise<void>

从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
path string 应用程序内部数据存储路径。

返回值:

类型 说明
Promise<void> Promise实例,用于异步获取结果。

示例:

let path = '/data/storage/el2/database'
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
    console.info("Removed storage from cache successfully.")
}).catch((err) => {
    console.info("Removed storage from cache failed with err: " + err)
})

Storage

提供获取和修改存储数据的接口。

getSync

getSync(key: string, defValue: ValueType): ValueType

获取键对应的值,如果值为null或者非默认值类型,返回默认数据。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。
defValue ValueType 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。

返回值:

类型 说明
ValueType 键对应的值,如果值为null或者非默认值类型,返回默认数据。

示例:

let value = storage.getSync('startup', 'default')
console.info("The value of startup is " + value)

get

get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void

获取键对应的值,如果值为null或者非默认值类型,返回默认数据。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。
defValue ValueType 默认返回值。支持number、string、boolean。
callback AsyncCallback<ValueType> 回调函数。

示例:

storage.get('startup', 'default', function(err, value) {
    if (err) {
        console.info("Get the value of startup failed with err: " + err)
        return
    }
    console.info("The value of startup is " + value)
})

get

get(key: string, defValue: ValueType): Promise<ValueType>

获取键对应的值,如果值为null或者非默认值类型,返默认数据。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。
defValue ValueType 默认返回值。支持number、string、boolean。

返回值:

类型 说明
Promise<ValueType> Promise实例,用于异步获取结果。

示例:

let promiseget = storage.get('startup', 'default')
promiseget.then((value) => {
    console.info("The value of startup is " + value)
}).catch((err) => {
    console.info("Get the value of startup failed with err: " + err)
})

putSync

putSync(key: string, value: ValueType): void

首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要修改的存储的key,不能为空。
value ValueType 存储的新值。支持number、string、boolean。

示例:

storage.putSync('startup', 'auto')

put

put(key: string, value: ValueType, callback: AsyncCallback<void>): void

首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要修改的存储的key,不能为空。
value ValueType 存储的新值。支持number、string、boolean。
callback AsyncCallback<void> 回调函数。

示例:

storage.put('startup', 'auto', function (err) {
    if (err) {
        console.info("Put the value of startup failed with err: " + err)
        return
    }
    console.info("Put the value of startup successfully.")
})

put

put(key: string, value: ValueType): Promise<void>

首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要修改的存储的key,不能为空。
value ValueType 存储的新值。支持number、string、boolean。

返回值:

类型 说明
Promise<void> Promise实例,用于异步处理。

示例:

let promiseput = storage.put('startup', 'auto')
promiseput.then(() => {
    console.info("Put the value of startup successfully.")
}).catch((err) => {
    console.info("Put the value of startup failed with err: " + err)
})

hasSync

hasSync(key: string): boolean

检查存储对象是否包含名为给定key的存储。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。

返回值:

类型 说明
boolean true 表示存在,false表示不存在。

示例:

let isExist = storage.hasSync('startup')
if (isExist) {
    console.info("The key of startup is contained.")
}

has

has(key: string, callback: AsyncCallback<boolean>): boolean

检查存储对象是否包含名为给定key的存储。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。
callback AsyncCallback<boolean> 回调函数。

返回值:

类型 说明
boolean true表示存在,false表示不存在。

示例:

storage.has('startup', function (err, isExist) {
    if (err) {
        console.info("Check the key of startup failed with err: " + err)
        return
    }
    if (isExist) {
        console.info("The key of startup is contained.")
    }
})

has

has(key: string): Promise<boolean>

检查存储对象是否包含名为给定key的存储。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。

返回值:

类型 说明
Promise<boolean> Promise实例,用于异步处理。

示例:

let promisehas = storage.has('startup')
promisehas.then((isExist) => {
    if (isExist) {
        console.info("The key of startup is contained.")
    }
}).catch((err) => {
    console.info("Check the key of startup failed with err: " + err)
})

deleteSync

deleteSync(key: string): void

从存储对象中删除名为给定key的存储。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称。它不能为空。

示例:

storage.deleteSync('startup')

delete

delete(key: string, callback: AsyncCallback<void>): void

从存储对象中删除名为给定key的存储。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称,不能为空。
callback AsyncCallback<void> 回调函数。

示例:

storage.delete('startup', function (err) {
    if (err) {
        console.info("Delete startup key failed with err: " + err)
        return
    }
    console.info("Deleted startup key successfully.")
})

delete

delete(key: string): Promise<void>

从存储对象删除名为给定key的存储。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要获取的存储key名称。

返回值:

类型 说明
Promise<void> Promise实例,用于异步处理。

示例:

let promisedel = storage.delete('startup')
promisedel.then(() => {
    console.info("Deleted startup key successfully.")
}).catch((err) => {
    console.info("Delete startup key failed with err: " + err)
})

flushSync

flushSync(): void

将当前storage对象中的修改保存到当前的storage,并同步存储到文件中。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

示例:

storage.flushSync()

flush

flush(callback: AsyncCallback<void>): void

将当前storage对象中的修改保存到当前的storage,并异步存储到文件中。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数。

示例:

storage.flush(function (err) {
    if (err) {
        console.info("Flush to file failed with err: " + err)
        return
    }
    console.info("Flushed to file successfully.")
})

flush

flush(): Promise<void>

将当前storage对象中的修改保存到当前的storage,并异步存储到文件中。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

返回值:

类型 说明
Promise<void> Promise实例,用于异步处理。

示例:

let promiseflush = storage.flush()
promiseflush.then(() => {
    console.info("Flushed to file successfully.")
}).catch((err) => {
    console.info("Flush to file failed with err: " + err)
})

clearSync

clearSync(): void

清除此存储对象中的所有存储。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

示例:

storage.clearSync()

clear

clear(callback: AsyncCallback<void>): void

清除此存储对象中的所有存储。使用callback方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数。

示例:

storage.clear(function (err) {
    if (err) {
        console.info("Clear to file failed with err: " + err)
        return
    }
    console.info("Cleared to file successfully.")
})

clear

clear(): Promise<void>

清除此存储对象中的所有存储。使用Promise方式返回结果,此方法为异步方法。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

返回值:

类型 说明
Promise<void> Promise实例,用于异步处理。

示例:

let promiseclear = storage.clear()
promiseclear.then(() => {
    console.info("Cleared to file successfully.")
}).catch((err) => {
    console.info("Clear to file failed with err: " + err)
})

on('change')

on(type: 'change', callback: Callback<StorageObserver>): void

订阅数据变更者类需要实现StorageObserver接口,订阅的key的值发生变更后,在执行flush/flushSync方法后,callback方法会被回调。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 说明
type string 事件类型,固定值'change',表示数据变更。
callback Callback<StorageObserver> 回调对象实例。

示例:

var observer = function (key) {
    console.info("The key of " + key + " changed.")
}
storage.on('change', observer)
storage.putSync('startup', 'auto')
storage.flushSync()  // observer will be called.

off('change')

off(type: 'change', callback: Callback<StorageObserver>): void

当不再进行订阅数据变更时,使用此接口取消订阅。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 说明
type string 事件类型,固定值'change',表示数据变更。
callback Callback<StorageObserver> 需要取消的回调对象实例。

示例:

var observer = function (key) {
    console.info("The key of " + key + " changed.")
}
storage.off('change', observer)

StorageObserver

系统能力: 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core

名称 参数类型 必填 说明
key string 变更的数据内容。

ValueType

用于表示允许的数据字段类型。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

名称 说明
number 表示值类型为数字。
string 表示值类型为字符。
boolean 表示值类型为布尔值。
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
OpenHarmony-3.2-Beta1

搜索帮助