Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
5e778b21
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
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看板
未验证
提交
5e778b21
编写于
7月 21, 2023
作者:
O
openharmony_ci
提交者:
Gitee
7月 21, 2023
浏览文件
操作
浏览文件
下载
差异文件
!20925 Modify preferences api document
Merge pull request !20925 from 常佳兴/master
上级
025ca66f
fb35a281
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
632 addition
and
10 deletion
+632
-10
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+577
-9
zh-cn/application-dev/reference/errorcodes/errorcode-preferences.md
...ication-dev/reference/errorcodes/errorcode-preferences.md
+55
-1
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
5e778b21
...
@@ -40,7 +40,7 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
...
@@ -40,7 +40,7 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
[Preferences](#preferences)
>
| 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误
码
。 |
| callback | AsyncCallback
<
[Preferences](#preferences)
>
| 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误
对象
。 |
**示例:**
**示例:**
...
@@ -159,6 +159,159 @@ class EntryAbility extends UIAbility {
...
@@ -159,6 +159,159 @@ class EntryAbility extends UIAbility {
}
}
```
```
## data_preferences.getPreferences<sup>10+</sup>
getPreferences(context: Context, options: Options, callback: AsyncCallback
<
Preferences
>
): void
获取Preferences实例,使用callback异步回调。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
| callback | AsyncCallback
<
[Preferences](#preferences)
>
| 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误对象。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
preferences
=
null
;
try
{
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
preferences
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## data_preferences.getPreferences<sup>10+</sup>
getPreferences(context: Context, options: Options): Promise
<
Preferences
>
获取Preferences实例,使用Promise异步回调。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ---------------------------------- |
| Promise
<
[Preferences](#preferences)
>
| Promise对象,返回Preferences实例。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
preferences
=
null
;
try
{
let
promise
=
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
preferences
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## data_preferences.deletePreferences
## data_preferences.deletePreferences
deletePreferences(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
deletePreferences(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
...
@@ -177,7 +330,7 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
...
@@ -177,7 +330,7 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误
对象
。 |
**错误码:**
**错误码:**
...
@@ -306,6 +459,159 @@ class EntryAbility extends UIAbility {
...
@@ -306,6 +459,159 @@ class EntryAbility extends UIAbility {
}
}
```
```
## data_preferences.deletePreferences<sup>10+</sup>
deletePreferences(context: Context, options: Options, callback: AsyncCallback
<
void
>
): void
从内存中移除指定的Preferences实例,使用callback异步回调。
若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------- |
| 15500010 | Failed to delete preferences file. |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
console
.
info
(
"
Succeeded in deleting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
console
.
info
(
"
Succeeded in deleting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## data_preferences.deletePreferences<sup>10+</sup>
deletePreferences(context: Context, options: Options): Promise
<
void
>
从内存中移除指定的Preferences实例,使用Promise异步回调。
若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------------------- |
| Promise
<
void
>
| 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------- |
| 15500010 | Failed to delete preferences file. |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
let
promise
=
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## data_preferences.removePreferencesFromCache
## data_preferences.removePreferencesFromCache
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
...
@@ -322,7 +628,7 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
...
@@ -322,7 +628,7 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误
对象
。 |
**示例:**
**示例:**
...
@@ -483,6 +789,163 @@ class EntryAbility extends UIAbility {
...
@@ -483,6 +789,163 @@ class EntryAbility extends UIAbility {
}
}
```
```
## data_preferences.removePreferencesFromCache<sup>10+</sup>
removePreferencesFromCache(context: Context, options: Options, callback: AsyncCallback
<
void
>
): void
从缓存中移除指定的Preferences实例,使用callback异步回调。
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
console
.
info
(
"
Succeeded in removing preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
console
.
info
(
"
Succeeded in removing preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## data_preferences.removePreferencesFromCache<sup>10+</sup>
removePreferencesFromCache(context: Context, options: Options): Promise
<
void
>
从缓存中移除指定的Preferences实例,使用Promise异步回调。
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| context | Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-inner-app-context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-inner-application-uiAbilityContext.md
)
。 |
| options |
[
Options
](
#options10
)
| 是 | 与Preferences实例相关的配置选项。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------------------- |
| Promise
<
void
>
| 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | ------------------------------ |
| 15501001 | Only supported in stage mode. |
| 15501002 | The data group id is not valid. |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
}
```
## Options<sup>10+</sup>
Preferences实例配置选项。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
| 名称 | 类型 | 必填 | 说明 |
| ----------- | ------ | ---- | ------------------------------------------------------------ |
| name | string | 是 | Preferences实例的名称。 |
| dataGroupId | string | 否 | 应用组ID,需要向应用市场获取。
<br/>
**模型约束:**
此属性仅在Stage模型下可用。
<br/>
从API version 10开始,支持此可选参数。指定在此dataGroupId对应的沙箱路径下创建Preferences实例,当此参数不填时,默认在本应用沙箱目录下创建Preferences实例。 |
## Preferences
## Preferences
首选项实例,提供获取和修改存储数据的接口。
首选项实例,提供获取和修改存储数据的接口。
...
@@ -504,7 +967,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>):
...
@@ -504,7 +967,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>):
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| key | string | 是 | 要获取的存储Key名称,不能为空。 |
| key | string | 是 | 要获取的存储Key名称,不能为空。 |
| defValue |
[
ValueType
](
#valuetype
)
| 是 | 默认返回值。支持number、string、boolean、Array
\<
number>、Array
\<
string>、Array
\<
boolean>类型。 |
| defValue |
[
ValueType
](
#valuetype
)
| 是 | 默认返回值。支持number、string、boolean、Array
\<
number>、Array
\<
string>、Array
\<
boolean>类型。 |
| callback | AsyncCallback
<
[ValueType](#valuetype)
>
| 是 | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误
码
。 |
| callback | AsyncCallback
<
[ValueType](#valuetype)
>
| 是 | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误
对象
。 |
**示例:**
**示例:**
...
@@ -603,7 +1066,7 @@ getAll(callback: AsyncCallback<Object>): void;
...
@@ -603,7 +1066,7 @@ getAll(callback: AsyncCallback<Object>): void;
| 参数名 | 类型 | 必填 | 说明 |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback
<
Object
>
| 是 | 回调函数。当获取成功,err为undefined,value为所有键值数据;否则err为错误
码
。 |
| callback | AsyncCallback
<
Object
>
| 是 | 回调函数。当获取成功,err为undefined,value为所有键值数据;否则err为错误
对象
。 |
**示例:**
**示例:**
...
@@ -696,7 +1159,7 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void
...
@@ -696,7 +1159,7 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| key | string | 是 | 要修改的存储的Key,不能为空。 |
| key | string | 是 | 要修改的存储的Key,不能为空。 |
| value |
[
ValueType
](
#valuetype
)
| 是 | 存储的新值。支持number、string、boolean、Array
\<
number>、Array
\<
string>、Array
\<
boolean>类型。 |
| value |
[
ValueType
](
#valuetype
)
| 是 | 存储的新值。支持number、string、boolean、Array
\<
number>、Array
\<
string>、Array
\<
boolean>类型。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当数据写入成功,err为undefined;否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当数据写入成功,err为undefined;否则为错误
对象
。 |
**示例:**
**示例:**
...
@@ -903,7 +1366,7 @@ delete(key: string, callback: AsyncCallback<void>): void
...
@@ -903,7 +1366,7 @@ delete(key: string, callback: AsyncCallback<void>): void
| 参数名 | 类型 | 必填 | 说明 |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| key | string | 是 | 要删除的存储Key名称,不能为空。 |
| key | string | 是 | 要删除的存储Key名称,不能为空。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当删除成功,err为undefined;否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当删除成功,err为undefined;否则为错误
对象
。 |
**示例:**
**示例:**
...
@@ -995,7 +1458,7 @@ flush(callback: AsyncCallback<void>): void
...
@@ -995,7 +1458,7 @@ flush(callback: AsyncCallback<void>): void
| 参数名 | 类型 | 必填 | 说明 |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当保存成功,err为undefined;否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当保存成功,err为undefined;否则为错误
对象
。 |
**示例:**
**示例:**
...
@@ -1056,7 +1519,7 @@ clear(callback: AsyncCallback<void>): void
...
@@ -1056,7 +1519,7 @@ clear(callback: AsyncCallback<void>): void
| 参数名 | 类型 | 必填 | 说明 |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当清除成功,err为undefined;否则为错误
码
。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当清除成功,err为undefined;否则为错误
对象
。 |
**示例:**
**示例:**
...
@@ -1173,6 +1636,62 @@ try {
...
@@ -1173,6 +1636,62 @@ try {
}
}
```
```
### on('multiProcessChange')<sup>10+</sup>
on(type: 'multiProcessChange', callback: Callback
<
{ key : string }
>
): void
订阅进程间数据变更,多个进程持有同一个首选项文件时,订阅的Key的值在任意一个进程发生变更后,执行
[
flush
](
#flush
)
方法后,触发callback回调。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------- | ---- | -------------------------------------------------------------- |
| type | string | 是 | 事件类型,固定值'multiProcessChange',表示多进程间的数据变更。 |
| callback | Callback
<
{ key : string }
>
| 是 | 回调对象实例。 |
**错误码:**
以下错误码的详细介绍请参见
[
用户首选项错误码
](
../errorcodes/errorcode-preferences.md
)
。
| 错误码ID | 错误信息 |
| -------- | -------------------------------------- |
| 15500019 | Failed to obtain subscription service. |
**示例:**
```
js
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
multiProcessChange
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
manual
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
})
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
### off('change')
### off('change')
...
@@ -1224,6 +1743,55 @@ try {
...
@@ -1224,6 +1743,55 @@ try {
}
}
```
```
### off('multiProcessChange')<sup>10+</sup>
off(type: 'multiProcessChange', callback?: Callback
<
{ key : string }
>
): void
取消订阅进程间数据变更。
**系统能力:**
SystemCapability.DistributedDataManager.Preferences.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------- | ---- | -------------------------------------------------------------- |
| type | string | 是 | 事件类型,固定值'multiProcessChange',表示多进程间的数据变更。 |
| callback | Callback
<
{ key : string }
>
| 否 | 需要取消的回调对象实例,不填写则全部取消。 |
**示例:**
```
js
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
multiProcessChange
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
preferences
.
off
(
'
multiProcessChange
'
,
observer
);
})
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
## ValueType
## ValueType
用于表示允许的数据字段类型。
用于表示允许的数据字段类型。
...
...
zh-cn/application-dev/reference/errorcodes/errorcode-preferences.md
浏览文件 @
5e778b21
...
@@ -22,4 +22,58 @@ Failed to delete preferences file.
...
@@ -22,4 +22,58 @@ Failed to delete preferences file.
**处理步骤**
**处理步骤**
1.
检查文件名称是否正确。
1.
检查文件名称是否正确。
2.
检查文件路径是否正确。
2.
检查文件路径是否正确。
\ No newline at end of file
## 15500019 获取订阅服务失败
**错误信息**
Failed to obtain subscription service.
**错误描述**
进行进程间订阅时,获取订阅服务失败。
**可能原因**
当前平台不支持订阅服务。
**处理步骤**
需要在当前平台部署订阅服务。
## 15501001 上下文环境非Stage模型
**错误信息**
Only supported in stage mode.
**错误描述**
该操作仅支持Stage模型。
**可能原因**
当前上下文环境非Stage模型。
**处理步骤**
请切换当前上下文环境,使用Stage模型。
## 15501002 Options中传入的dataGroupId参数非法
**错误信息**
The data group id is not valid.
**错误描述**
使用非法dataGroupId参数。
**可能原因**
使用的dataGroupId不是从应用市场正常申请的。
**处理步骤**
应用从应用市场申请dataGroupId,并正确传入该参数。
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录