Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
96bba2b9
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
96bba2b9
编写于
3年前
作者:
P
PaDaBoo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Change Storage to Preferences
Signed-off-by:
N
PaDaBoo
<
xuejianwu@huawei.com
>
上级
689ea9bb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
35 deletion
+43
-35
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+42
-34
zh-cn/application-dev/reference/apis/js-apis-data-storage.md
zh-cn/application-dev/reference/apis/js-apis-data-storage.md
+1
-1
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
96bba2b9
...
...
@@ -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) => {
...
...
This diff is collapsed.
Click to expand it.
zh-cn/application-dev/reference/apis/js-apis-data-storage.md
浏览文件 @
96bba2b9
...
...
@@ -4,7 +4,7 @@
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
>
本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>
从API Version 8开始,该接口不再维护,推荐使用新接口'@ohos.data.preferences'
## 导入模块
...
...
This diff is collapsed.
Click to expand it.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录
新手
引导
客服
返回
顶部