From 689ea9bbb2e02adaf3c3df7dc733ee2374b459f1 Mon Sep 17 00:00:00 2001 From: PaDoBoo Date: Fri, 18 Feb 2022 18:57:50 +0800 Subject: [PATCH 1/5] Change storage to preferences Signed-off-by: PaDoBoo --- .../apis/js-apis-data-preferences.md | 578 ++++++++++++++++++ 1 file changed, 578 insertions(+) create mode 100644 zh-cn/application-dev/reference/apis/js-apis-data-preferences.md diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md new file mode 100644 index 00000000000..359712e7536 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -0,0 +1,578 @@ +# 轻量级存储 + +轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。 + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 + + +## 导入模块 + +``` +import dataPreferences from '@ohos.data.preferences' +``` + + +## 权限 + +无 + + +## 属性 + +| 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 | +| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 | + + +## dataPreferences.getPreferences + +getPreferences(path: string, callback: AsyncCallback<Preferences>): void + +读取指定文件,将数据加载到Preferences实例,用于数据操作,使用callback形式返回结果。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | + | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 | + +- 示例: + ``` + import dataPreferences from '@ohos.data.preferences' + import Ability from '@ohos.application.Ability' + + var context = featureAbility.getContext() + var path = await context.getDataBaseDir() + dataPreferences.getPreferences(this.context, 'mystore', function (err, preferences) { + if (err) { + console.info("Get the preferences failed, path: " + path + '/mystore') + return; + } + preferences.putSync('startup', 'auto') + preferences.flushSync() + }) + ``` + + +## dataPreferences.getPreferences + +getPreferences(path: string): Promise<Preferences> + +读取指定文件,将数据加载到Preferences实例,用于数据操作,使用Promise方式作为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 | + +- 示例: + ``` + import dataPreferences from '@ohos.data.preferences' + import featureAbility from '@ohos.ability.featureAbility' + + var context = featureAbility.getContext() + var path = await context.getDataBaseDir() + let promise = dataPreferences.getPreferences(path + '/mystore') + promise.then((preferences) => { + preferences.putSync('startup', 'auto') + preferences.flushSync() + }).catch((err) => { + console.info("Get the preferences failed, path: " + path + '/mystore') + }) + ``` + + +## dataPreferences.deletePreferences + +deletePreferences(path: string, callback: AsyncCallback<void>) + +从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式作为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | path | string | 是 | 应用程序内部数据存储路径。 | + | callback | AsyncCallback<void> | 是 | 回调函数。 | + +- 示例: + ``` + dataPreferences.deletePreferences(path + '/mystore', function (err) { + if (err) { + console.info("Deleted failed with err: " + err) + return + } + console.info("Deleted successfully.") + }) + ``` + + +## dataPreferences.deletePreferences + +deletePreferences(path: string): Promise<void> + +从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式作为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | path | string | 是 | 应用程序内部数据存储路径。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise实例,用于异步获取结果。 | + +- 示例: + ``` + let promise = dataPreferences.deletePreferences(path + '/mystore') + promise.then(() => { + console.info("Deleted successfully.") + }).catch((err) => { + console.info("Deleted failed with err: " + err) + }) + ``` + + +## dataPreferences.removePreferencesFromCache + +removePreferencesFromCache(path: string, callback: AsyncCallback<Preferences>): void + +从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | path | string | 是 | 应用程序内部数据存储路径。 | + | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 | + +- 示例: + ``` + dataPreferences.removePreferencesFromCache(path + '/mystore', function (err) { + if (err) { + console.info("Removed preferences from cache failed with err: " + err) + return + } + console.info("Removed preferences from cache successfully.") + }) + ``` + + +## dataPreferences.removePreferencesFromCache + +removePreferencesFromCache(path: string): Promise<void> + +从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | path | string | 是 | 应用程序内部数据存储路径。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise实例,用于异步获取结果。 | + +- 示例: + ``` + let promise = dataPreferences.removePreferencesFromCache(path + '/mystore') + promise.then(() => { + console.info("Removed preferences from cache successfully.") + }).catch((err) => { + console.info("Removed preferences from cache failed with err: " + err) + }) + ``` + + +## Preferences + +提供获取和修改存储数据的接口。 + + +### get + +get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void + +获取键对应的值,如果值为null或者非默认值类型,返回默认数据。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称。它不能为空。 | + | defValue | ValueType | 是 | 默认返回值。支持number、string、boolean。 | + | callback | AsyncCallback<ValueType> | 是 | 回调函数。 | + +- 示例: + ``` + preferences.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或者非默认值类型,返默认数据。 + +此方法为异步方法。 + +- **参数:** + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称。它不能为空。 | + | defValue | ValueType | 是 | 默认返回值。支持number、string、boolean。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<ValueType> | Promise实例,用于异步获取结果。 | + +- 示例: + ``` + let promise = preferences.get('startup', 'default') + promise.then((value) => { + console.info("The value of startup is " + value) + }).catch((err) => { + console.info("Get the value of startup failed with err: " + err) + }) + ``` + + +### put + +put(key: string, value: ValueType, callback: AsyncCallback<void>): void + +首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要修改的存储的key。它不能为空。 | + | value | ValueType | 是 | 存储的新值。支持number、string、boolean。 | + | callback | AsyncCallback<void> | 是 | 回调函数。 | + +- 示例: + ``` + preferences.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> + +首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要修改的存储的key。它不能为空。 | + | value | ValueType | 是 | 存储的新值。支持number、string、boolean。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise实例,用于异步处理。 | + +- 示例: + ``` + let promise = preferences.put('startup', 'auto') + promise.then(() => { + console.info("Put the value of startup successfully.") + }).catch((err) => { + console.info("Put the value of startup failed with err: " + err) + }) + ``` + + +### has + +has(key: string, callback: AsyncCallback<boolean>): boolean + +检查存储对象是否包含名为给定key的存储。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称,不能为空。 | + | callback | AsyncCallback<boolean> | 是 | 回调函数。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | boolean | true表示存在,false表示不存在。 | + +- 示例: + ``` + preferences.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的存储。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称。它不能为空。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<boolean> | Promise实例,用于异步处理。 | + +- 示例: + ``` + let promise = preferences.has('startup') + promise.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) + }) + ``` + + +### delete + +delete(key: string, callback: AsyncCallback<void>): void + +从存储对象中删除名为给定key的存储。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称,不能为空。 | + | callback | AsyncCallback<void> | 是 | 回调函数。 | + +- 示例: + ``` + preferences.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的存储。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | key | string | 是 | 要获取的存储key名称。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise实例,用于异步处理。 | + +- 示例: + ``` + let promise = preferences.delete('startup') + promise.then(() => { + console.info("Deleted startup key successfully.") + }).catch((err) => { + console.info("Delete startup key failed with err: " + err) + }) + ``` + + +### flush + +flush(callback: AsyncCallback<void>): void + +将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | callback | AsyncCallback<void> | 是 | 回调函数。 | + +- 示例: + ``` + preferences.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> + +将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。 + +此方法为异步方法。 + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise实例,用于异步处理。 | + +- 示例: + ``` + let promise = preferences.flush() + promise.then(() => { + console.info("Flushed to file successfully.") + }).catch((err) => { + console.info("Flush to file failed with err: " + err) + }) + ``` + + +### clear + +clear(callback: AsyncCallback<void>): void + +清除此存储对象中的所有存储。 + +此方法为异步方法。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | callback | AsyncCallback<void> | 是 | 回调函数。 | + +- 示例: + ``` + preferences.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<void> | Promise实例,用于异步处理。 | + +- 示例: + ``` + let promise = preferences.clear() + promise.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<{ key : string }>): void + +订阅数据变更者类,订阅的key的值发生变更后,在执行flush方法后,callback方法会被回调。 + +- 参数: + | 参数名 | 类型 | 说明 | + | -------- | -------- | -------- | + | type | string | 事件类型,固定值'change',表示数据变更。 | + | callback | Callback<{ key : string }> | 回调对象实例。 | + +- 示例: + ``` + var observer = function (key) { + console.info("The key of " + key + " changed.") + } + preferences.on('change', observer) + preferences.put('startup', 'auto') + preferences.flush() // observer will be called. + ``` + + +### off('change') + +off(type: 'change', callback: Callback<{ key : string }>): void + +当不再进行订阅数据变更时,使用此接口取消订阅。 + +- 参数: + | 参数名 | 类型 | 说明 | + | -------- | -------- | -------- | + | type | string | 事件类型,固定值'change',表示数据变更。 | + | callback | Callback<{ key : string }> | 需要取消的回调对象实例。 | + +- 示例: + ``` + var observer = function (key) { + console.info("The key of " + key + " changed.") + } + preferences.off('change', observer) + ``` -- Gitee From 96bba2b9752b919416fe349ecb900401d38d6e5a Mon Sep 17 00:00:00 2001 From: PaDaBoo Date: Sat, 19 Feb 2022 15:50:24 +0800 Subject: [PATCH 2/5] Change Storage to Preferences Signed-off-by: PaDaBoo --- .../apis/js-apis-data-preferences.md | 76 ++++++++++--------- .../reference/apis/js-apis-data-storage.md | 2 +- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md index 359712e7536..f693d6687ae 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -4,13 +4,13 @@ > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** -> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ``` -import dataPreferences from '@ohos.data.preferences' +import data_Preferences from '@ohos.data.preferences' ``` @@ -27,9 +27,9 @@ import dataPreferences from '@ohos.data.preferences' | MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 | -## dataPreferences.getPreferences +## data_Preferences.getPreferences -getPreferences(path: string, callback: AsyncCallback<Preferences>): void +getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void 读取指定文件,将数据加载到Preferences实例,用于数据操作,使用callback形式返回结果。 @@ -42,12 +42,10 @@ getPreferences(path: string, callback: AsyncCallback<Preferences>): void - 示例: ``` - import dataPreferences from '@ohos.data.preferences' import Ability from '@ohos.application.Ability' - - var context = featureAbility.getContext() - var path = await context.getDataBaseDir() - dataPreferences.getPreferences(this.context, 'mystore', function (err, preferences) { + import data_Preferences from '@ohos.data.preferences' + var path = await this.context.getDataBaseDir() + data_Preferences.getPreferences(this.context, 'mystore', function (err, preferences) { if (err) { console.info("Get the preferences failed, path: " + path + '/mystore') return; @@ -58,9 +56,9 @@ getPreferences(path: string, callback: AsyncCallback<Preferences>): void ``` -## dataPreferences.getPreferences +## data_Preferences.getPreferences -getPreferences(path: string): Promise<Preferences> +getPreferences(context: Context, name: string): Promise<Preferences> 读取指定文件,将数据加载到Preferences实例,用于数据操作,使用Promise方式作为异步方法。 @@ -77,12 +75,10 @@ getPreferences(path: string): Promise<Preferences> - 示例: ``` - import dataPreferences from '@ohos.data.preferences' - import featureAbility from '@ohos.ability.featureAbility' - - var context = featureAbility.getContext() - var path = await context.getDataBaseDir() - let promise = dataPreferences.getPreferences(path + '/mystore') + import Ability from '@ohos.application.Ability' + import data_Preferences from '@ohos.data.preferences' + var path = await this.context.getDataBaseDir() + let promise = data_Preferences.getPreferences(this.context, 'mystore') promise.then((preferences) => { preferences.putSync('startup', 'auto') preferences.flushSync() @@ -92,21 +88,24 @@ getPreferences(path: string): Promise<Preferences> ``` -## dataPreferences.deletePreferences +## data_Preferences.deletePreferences -deletePreferences(path: string, callback: AsyncCallback<void>) +deletePreferences(context: Context, name: string, callback: AsyncCallback<void>) 从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式作为异步方法。 - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | path | string | 是 | 应用程序内部数据存储路径。 | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | | callback | AsyncCallback<void> | 是 | 回调函数。 | - 示例: - ``` - dataPreferences.deletePreferences(path + '/mystore', function (err) { + ``` + import Ability from '@ohos.application.Ability' + import data_Preferences from '@ohos.data.preferences' + data_Preferences.deletePreferences(this.context, 'mystore', function (err) { if (err) { console.info("Deleted failed with err: " + err) return @@ -116,16 +115,17 @@ deletePreferences(path: string, callback: AsyncCallback<void>) ``` -## dataPreferences.deletePreferences +## data_Preferences.deletePreferences -deletePreferences(path: string): Promise<void> +deletePreferences(context: Context, name: string): Promise<void> 从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式作为异步方法。 - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | path | string | 是 | 应用程序内部数据存储路径。 | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | - 返回值: | 类型 | 说明 | @@ -134,7 +134,9 @@ deletePreferences(path: string): Promise<void> - 示例: ``` - let promise = dataPreferences.deletePreferences(path + '/mystore') + import Ability from '@ohos.application.Ability' + import data_Preferences from '@ohos.data.preferences' + let promise = data_Preferences.deletePreferences(this.context, 'mystore') promise.then(() => { console.info("Deleted successfully.") }).catch((err) => { @@ -143,9 +145,9 @@ deletePreferences(path: string): Promise<void> ``` -## dataPreferences.removePreferencesFromCache +## data_Preferences.removePreferencesFromCache -removePreferencesFromCache(path: string, callback: AsyncCallback<Preferences>): void +removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<Preferences>): void 从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 @@ -154,12 +156,15 @@ removePreferencesFromCache(path: string, callback: AsyncCallback<Preferences& - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | path | string | 是 | 应用程序内部数据存储路径。 | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 | - 示例: ``` - dataPreferences.removePreferencesFromCache(path + '/mystore', function (err) { + import Ability from '@ohos.application.Ability' + import data_Preferences from '@ohos.data.preferences' + data_Preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { if (err) { console.info("Removed preferences from cache failed with err: " + err) return @@ -169,9 +174,9 @@ removePreferencesFromCache(path: string, callback: AsyncCallback<Preferences& ``` -## dataPreferences.removePreferencesFromCache +## data_Preferences.removePreferencesFromCache -removePreferencesFromCache(path: string): Promise<void> +removePreferencesFromCache(context: Context, name: string): Promise<void> 从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 @@ -180,7 +185,8 @@ removePreferencesFromCache(path: string): Promise<void> - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | path | string | 是 | 应用程序内部数据存储路径。 | + | context | Context | 是 | 应用程序或功能的上下文 | + | name | string | 是 | 应用程序内部数据存储名称。 | - 返回值: | 类型 | 说明 | @@ -189,7 +195,9 @@ removePreferencesFromCache(path: string): Promise<void> - 示例: ``` - let promise = dataPreferences.removePreferencesFromCache(path + '/mystore') + import Ability from '@ohos.application.Ability' + import data_Preferences from '@ohos.data.preferences' + let promise = data_Preferences.removePreferencesFromCache(this.context, 'mystore') promise.then(() => { console.info("Removed preferences from cache successfully.") }).catch((err) => { diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md index 37f09d0cbf1..ad0a05f29d5 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md @@ -4,7 +4,7 @@ > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** -> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +> 从API Version 8开始,该接口不再维护,推荐使用新接口'@ohos.data.preferences' ## 导入模块 -- Gitee From c80c4d7b447260f68d289702d5ff879b476553bb Mon Sep 17 00:00:00 2001 From: PaDaBoo Date: Sat, 19 Feb 2022 17:11:43 +0800 Subject: [PATCH 3/5] change storage to preferences Signed-off-by: PaDaBoo --- zh-cn/application-dev/reference/apis/Readme-CN.md | 3 ++- zh-cn/website-directory.md | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/Readme-CN.md b/zh-cn/application-dev/reference/apis/Readme-CN.md index c26de29843c..0bb346f1816 100644 --- a/zh-cn/application-dev/reference/apis/Readme-CN.md +++ b/zh-cn/application-dev/reference/apis/Readme-CN.md @@ -22,7 +22,8 @@ - [用户认证](js-apis-useriam-userauth.md) - [访问控制](js-apis-security-accessToken.md) - 数据管理 - - [轻量级存储](js-apis-data-storage.md) + - [轻量级存储](js-apis-data-preferences.md) + - [轻量级存储(废弃 since 8)](js-apis-data-storage.md) - [分布式数据管理](js-apis-distributed-data.md) - [关系型数据库](js-apis-data-rdb.md) - [结果集](js-apis-data-resultset.md) diff --git a/zh-cn/website-directory.md b/zh-cn/website-directory.md index 67cc8979d83..bb43e492dda 100644 --- a/zh-cn/website-directory.md +++ b/zh-cn/website-directory.md @@ -1378,7 +1378,9 @@ ——>——>——>——> 数据管理 -——>——>——>——>——> [轻量级存储](application-dev/reference/apis/js-apis-data-storage.md) +——>——>——>——>——> [轻量级存储](application-dev/reference/apis/js-apis-data-preferences.md) + +——>——>——>——>——> [轻量级存储(废弃 since 8)](application-dev/reference/apis/js-apis-data-storage.md) ——>——>——>——>——> [分布式数据管理](application-dev/reference/apis/js-apis-distributed-data.md) -- Gitee From b73c9f639887fb1483d5f45752715df82a629a7e Mon Sep 17 00:00:00 2001 From: PaDaBoo Date: Tue, 22 Feb 2022 16:13:28 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=A1=A5=E5=85=85sysCap=E8=83=BD=E5=8A=9B?= =?UTF-8?q?=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: PaDaBoo --- .../application-dev/reference/apis/js-apis-data-preferences.md | 2 ++ zh-cn/application-dev/reference/apis/js-apis-data-storage.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md index f693d6687ae..5c97b056f78 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -13,6 +13,8 @@ import data_Preferences from '@ohos.data.preferences' ``` +## 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core ## 权限 diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md index ad0a05f29d5..b70fb714ffb 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md @@ -13,6 +13,8 @@ import dataStorage from '@ohos.data.storage' ``` +## 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core ## 权限 -- Gitee From 342ace09158d5f4448088b9f91d0a4fc36cae60a Mon Sep 17 00:00:00 2001 From: PaDaBoo Date: Tue, 22 Feb 2022 19:41:05 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=8C=89=E7=85=A7=E6=9C=80=E6=96=B0API?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AF=B4=E6=98=8E=E6=A8=A1=E6=9D=BF=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: PaDaBoo --- .../apis/js-apis-data-preferences.md | 59 +++++++++++++--- .../reference/apis/js-apis-data-storage.md | 70 +++++++++---------- 2 files changed, 83 insertions(+), 46 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md index 5c97b056f78..543b429ce1a 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -13,24 +13,19 @@ import data_Preferences from '@ohos.data.preferences' ``` -## 系统能力 -SystemCapability.DistributedDataManager.Preferences.Core - -## 权限 - -无 - - ## 属性 | 名称 | 参数类型 | 可读 | 可写 | 说明 | | -------- | -------- | -------- | -------- | -------- | -| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 | -| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 | +| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core | +| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core | ## data_Preferences.getPreferences +### 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core + getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void 读取指定文件,将数据加载到Preferences实例,用于数据操作,使用callback形式返回结果。 @@ -60,6 +55,9 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer ## data_Preferences.getPreferences +### 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core + getPreferences(context: Context, name: string): Promise<Preferences> 读取指定文件,将数据加载到Preferences实例,用于数据操作,使用Promise方式作为异步方法。 @@ -92,6 +90,9 @@ getPreferences(context: Context, name: string): Promise<Preferences> ## data_Preferences.deletePreferences +### 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core + deletePreferences(context: Context, name: string, callback: AsyncCallback<void>) 从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式作为异步方法。 @@ -104,7 +105,7 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi | callback | AsyncCallback<void> | 是 | 回调函数。 | - 示例: - ``` + ``` import Ability from '@ohos.application.Ability' import data_Preferences from '@ohos.data.preferences' data_Preferences.deletePreferences(this.context, 'mystore', function (err) { @@ -119,6 +120,9 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi ## data_Preferences.deletePreferences +### 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core + deletePreferences(context: Context, name: string): Promise<void> 从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式作为异步方法。 @@ -149,6 +153,9 @@ deletePreferences(context: Context, name: string): Promise<void> ## data_Preferences.removePreferencesFromCache +### 系统能力 +SystemCapability.DistributedDataManager.Preferences.Core + removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<Preferences>): void 从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 @@ -178,6 +185,8 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba ## data_Preferences.removePreferencesFromCache +### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + removePreferencesFromCache(context: Context, name: string): Promise<void> 从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 @@ -215,6 +224,8 @@ removePreferencesFromCache(context: Context, name: string): Promise<void> ### get +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void 获取键对应的值,如果值为null或者非默认值类型,返回默认数据。 @@ -242,6 +253,8 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): ### get +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + get(key: string, defValue: ValueType): Promise<ValueType> 获取键对应的值,如果值为null或者非默认值类型,返默认数据。 @@ -272,6 +285,8 @@ get(key: string, defValue: ValueType): Promise<ValueType> ### put +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + put(key: string, value: ValueType, callback: AsyncCallback<void>): void 首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。 @@ -299,6 +314,8 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void ### put +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + put(key: string, value: ValueType): Promise<void> 首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。 @@ -329,6 +346,8 @@ put(key: string, value: ValueType): Promise<void> ### has +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + has(key: string, callback: AsyncCallback<boolean>): boolean 检查存储对象是否包含名为给定key的存储。 @@ -362,6 +381,8 @@ has(key: string, callback: AsyncCallback<boolean>): boolean ### has +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + has(key: string): Promise<boolean> 检查存储对象是否包含名为给定key的存储。 @@ -393,6 +414,8 @@ has(key: string): Promise<boolean> ### delete +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + delete(key: string, callback: AsyncCallback<void>): void 从存储对象中删除名为给定key的存储。 @@ -419,6 +442,8 @@ delete(key: string, callback: AsyncCallback<void>): void ### delete +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + delete(key: string): Promise<void> 从存储对象删除名为给定key的存储。 @@ -448,6 +473,8 @@ delete(key: string): Promise<void> ### flush +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + flush(callback: AsyncCallback<void>): void 将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。 @@ -473,6 +500,8 @@ flush(callback: AsyncCallback<void>): void ### flush +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + flush(): Promise<void> 将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。 @@ -497,6 +526,8 @@ flush(): Promise<void> ### clear +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + clear(callback: AsyncCallback<void>): void 清除此存储对象中的所有存储。 @@ -522,6 +553,8 @@ clear(callback: AsyncCallback<void>): void ### clear +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + clear(): Promise<void> 清除此存储对象中的所有存储。 @@ -546,6 +579,8 @@ clear(): Promise<void> ### on('change') +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + on(type: 'change', callback: Callback<{ key : string }>): void 订阅数据变更者类,订阅的key的值发生变更后,在执行flush方法后,callback方法会被回调。 @@ -569,6 +604,8 @@ on(type: 'change', callback: Callback<{ key : string }>): void ### off('change') +#### 系统能力 SystemCapability.DistributedDataManager.Preferences.Core + off(type: 'change', callback: Callback<{ key : string }>): void 当不再进行订阅数据变更时,使用此接口取消订阅。 diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md index b70fb714ffb..04a75385396 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-storage.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-storage.md @@ -4,7 +4,7 @@ > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** -> 从API Version 8开始,该接口不再维护,推荐使用新接口'@ohos.data.preferences' +> 从API Version 8开始,该接口不再维护,推荐使用新接口 [@ohos.data.preferences](js-apis-data-preferences.md) ## 导入模块 @@ -29,7 +29,7 @@ SystemCapability.DistributedDataManager.Preferences.Core | MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 | -## dataStorage.getStorageSync +## dataStorage.getStorageSync(deprecated) getStorageSync(path: string): Storage @@ -49,7 +49,7 @@ getStorageSync(path: string): Storage ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' - + var context = featureAbility.getContext() var path = await context.getFilesDir() let storage = dataStorage.getStorageSync(path + '/mystore') @@ -58,7 +58,7 @@ getStorageSync(path: string): Storage ``` -## dataStorage.getStorage +## dataStorage.getStorage(deprecated) getStorage(path: string, callback: AsyncCallback<Storage>): void @@ -74,7 +74,7 @@ getStorage(path: string, callback: AsyncCallback<Storage>): void ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' - + var context = featureAbility.getContext() var path = await context.getFilesDir() dataStorage.getStorage(path + '/mystore', function (err, storage) { @@ -88,7 +88,7 @@ getStorage(path: string, callback: AsyncCallback<Storage>): void ``` -## dataStorage.getStorage +## dataStorage.getStorage(deprecated) getStorage(path: string): Promise<Storage> @@ -108,7 +108,7 @@ getStorage(path: string): Promise<Storage> ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' - + var context = featureAbility.getContext() var path = await context.getFilesDir() let promise = dataStorage.getStorage(path + '/mystore') @@ -121,7 +121,7 @@ getStorage(path: string): Promise<Storage> ``` -## dataStorage.deleteStorageSync +## dataStorage.deleteStorageSync(deprecated) deleteStorageSync(path: string): void @@ -138,7 +138,7 @@ deleteStorageSync(path: string): void ``` -## dataStorage.deleteStorage +## dataStorage.deleteStorage(deprecated) deleteStorage(path: string, callback: AsyncCallback<void>) @@ -162,7 +162,7 @@ deleteStorage(path: string, callback: AsyncCallback<void>) ``` -## dataStorage.deleteStorage +## dataStorage.deleteStorage(deprecated) deleteStorage(path: string): Promise<void> @@ -189,7 +189,7 @@ deleteStorage(path: string): Promise<void> ``` -## dataStorage.removeStorageFromCacheSync +## dataStorage.removeStorageFromCacheSync(deprecated) removeStorageFromCacheSync(path: string): void @@ -208,7 +208,7 @@ removeStorageFromCacheSync(path: string): void ``` -## dataStorage.removeStorageFromCache +## dataStorage.removeStorageFromCache(deprecated) removeStorageFromCache(path: string, callback: AsyncCallback<Storage>): void @@ -234,7 +234,7 @@ removeStorageFromCache(path: string, callback: AsyncCallback<Storage>): vo ``` -## dataStorage.removeStorageFromCache +## dataStorage.removeStorageFromCache(deprecated) removeStorageFromCache(path: string): Promise<void> @@ -263,12 +263,12 @@ removeStorageFromCache(path: string): Promise<void> ``` -## Storage +## Storage(deprecated) 提供获取和修改存储数据的接口。 -### getSync +### getSync(deprecated) getSync(key: string, defValue: ValueType): ValueType @@ -294,7 +294,7 @@ getSync(key: string, defValue: ValueType): ValueType ``` -### get +### get(deprecated) get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void @@ -321,7 +321,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): ``` -### get +### get(deprecated) get(key: string, defValue: ValueType): Promise<ValueType> @@ -351,7 +351,7 @@ get(key: string, defValue: ValueType): Promise<ValueType> ``` -### putSync +### putSync(deprecated) putSync(key: string, value: ValueType): void @@ -371,7 +371,7 @@ putSync(key: string, value: ValueType): void ``` -### put +### put(deprecated) put(key: string, value: ValueType, callback: AsyncCallback<void>): void @@ -398,7 +398,7 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void ``` -### put +### put(deprecated) put(key: string, value: ValueType): Promise<void> @@ -428,7 +428,7 @@ put(key: string, value: ValueType): Promise<void> ``` -### hasSync +### hasSync(deprecated) hasSync(key: string): boolean @@ -455,7 +455,7 @@ hasSync(key: string): boolean ``` -### has +### has(deprecated) has(key: string, callback: AsyncCallback<boolean>): boolean @@ -488,7 +488,7 @@ has(key: string, callback: AsyncCallback<boolean>): boolean ``` -### has +### has(deprecated) has(key: string): Promise<boolean> @@ -519,7 +519,7 @@ has(key: string): Promise<boolean> ``` -### deleteSync +### deleteSync(deprecated) deleteSync(key: string): void @@ -538,7 +538,7 @@ deleteSync(key: string): void ``` -### delete +### delete(deprecated) delete(key: string, callback: AsyncCallback<void>): void @@ -564,7 +564,7 @@ delete(key: string, callback: AsyncCallback<void>): void ``` -### delete +### delete(deprecated) delete(key: string): Promise<void> @@ -593,7 +593,7 @@ delete(key: string): Promise<void> ``` -### flushSync +### flushSync(deprecated) flushSync(): void @@ -607,7 +607,7 @@ flushSync(): void ``` -### flush +### flush(deprecated) flush(callback: AsyncCallback<void>): void @@ -632,7 +632,7 @@ flush(callback: AsyncCallback<void>): void ``` -### flush +### flush(deprecated) flush(): Promise<void> @@ -656,7 +656,7 @@ flush(): Promise<void> ``` -### clearSync +### clearSync(deprecated) clearSync(): void @@ -670,7 +670,7 @@ clearSync(): void ``` -### clear +### clear(deprecated) clear(callback: AsyncCallback<void>): void @@ -695,7 +695,7 @@ clear(callback: AsyncCallback<void>): void ``` -### clear +### clear(deprecated) clear(): Promise<void> @@ -719,7 +719,7 @@ clear(): Promise<void> ``` -### on('change') +### on('change')(deprecated) on(type: 'change', callback: Callback<StorageObserver>): void @@ -742,7 +742,7 @@ on(type: 'change', callback: Callback<StorageObserver>): void ``` -### off('change') +### off('change')(deprecated) off(type: 'change', callback: Callback<StorageObserver>): void @@ -763,7 +763,7 @@ off(type: 'change', callback: Callback<StorageObserver>): void ``` -## StorageObserver +## StorageObserver(deprecated) | 名称 | 参数类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | -- Gitee