Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
9c9d8c4e
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看板
提交
9c9d8c4e
编写于
10月 18, 2022
作者:
U
unknown
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【data_object】API参考修改&错误码文档
Signed-off-by:
N
unknown
<
wangxiyue8@huawei.com
>
上级
06e869f4
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
672 addition
and
44 deletion
+672
-44
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
...tion-dev/reference/apis/js-apis-data-distributedobject.md
+653
-44
zh-cn/application-dev/reference/errorcodes/errorcode-distributed-data_object.md
...reference/errorcodes/errorcode-distributed-data_object.md
+19
-0
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
浏览文件 @
9c9d8c4e
...
...
@@ -13,35 +13,57 @@
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
```
## distributedDataObject.createDistributedObject
createDistributedObject(source: object): DistributedObject
## distributedObject.create<sup>9+</sup>
create(context: Context, source: object): DistributedObjectV9
创建一个分布式数据对象。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context | Context | 是 | 应用的上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| source | object | 是 | 设置分布式数据对象的属性。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
|
[
DistributedObject
](
#distributedobject
)
| 创建完成的分布式数据对象。 |
|
[
DistributedObject
V9
](
#distributedobjectv9
)
| 创建完成的分布式数据对象。 |
**示例:**
FA模型示例:
```
js
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
// 创建对象,对象包含4个属性类型,string,number,boolean和Object
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
// 创建对象,该对象包含4个属性类型,string,number,boolean和Object
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
```
Stage模型示例:
```
ts
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
// 创建对象,该对象包含4个属性类型,string,number,boolean和Object
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
```
## distributedObject.genSessionId
...
...
@@ -49,7 +71,7 @@ genSessionId(): string
随机创建一个sessionId。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**返回值:**
...
...
@@ -61,14 +83,14 @@ genSessionId(): string
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
sessionId
=
distributedObject
.
genSessionId
();
let
sessionId
=
distributedObject
.
genSessionId
();
```
## SaveSuccessResponse<sup>9+</sup>
save接口回调信息。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
| 名称 | 类型 | 说明 |
| -------- | -------- | -------- |
...
...
@@ -80,25 +102,150 @@ save接口回调信息。
revokeSave接口回调信息。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
| 名称 | 类型 | 说明 |
| -------- | -------- | -------- |
| sessionId | string | 多设备协同的唯一标识。 |
## DistributedObject
## DistributedObject
V9
表示一个分布式数据对象。
### setSessionId
### setSessionId
<sup>9+</sup>
setSessionId(sessionId?: string): boolean
setSessionId(sessionId: string, callback: AsyncCallback
<
void
>
): void
设置同步的sessionId,当可信组网中有多个设备时,多个设备间的对象如果设置为同一个sessionId,就能自动同步。
**需要权限:**
ohos.permission.DISTRIBUTED_DATASYNC。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| sessionId | string | 是 | 分布式数据对象在可信组网中的标识ID。|
| callback | AsyncCallback
<
void
>
| 是 | 加入session的异步回调。|
**错误码:**
以下错误码的详细介绍参见
[
分布式数据对象错误码
](
../errorcodes/errorcode-distributed-data_object.md
)
。
| 类型 | 说明 |
| -------- | -------- |
| 15400001 | 创建内存数据库失败。|
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
(),
()
=>
{
console
.
log
(
"
join session
"
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
(),
()
=>
{
console
.
log
(
"
join session
"
);
});
```
### setSessionId<sup>9+</sup>
setSessionId(callback: AsyncCallback
<
void
>
): void
退出所有已加入的session。
**需要权限:**
ohos.permission.DISTRIBUTED_DATASYNC。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback
<
void
>
| 是 | 退出所有已加入session的异步回调。 |
**错误码:**
以下错误码的详细介绍参见
[
分布式数据对象错误码
](
../errorcodes/errorcode-distributed-data_object.md
)
。
| 类型 | 说明 |
| -------- | -------- |
| 15400001 | 创建内存数据库失败。|
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
(),
()
=>
{
console
.
log
(
"
join session
"
);
});
// 退出分布式组网
g_object
.
setSessionId
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
(),
()
=>
{
console
.
log
(
"
join session
"
);
});
// 退出分布式组网
g_object
.
setSessionId
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
});
```
### setSessionId<sup>9+</sup>
setSessionId(sessionId?: string): Promise
<
void
>
设置同步的sessionId,当可信组网中有多个设备时,多个设备间的对象如果设置为同一个sessionId,就能自动同步。
**需要权限:**
ohos.permission.DISTRIBUTED_DATASYNC。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -108,28 +255,75 @@ setSessionId(sessionId?: string): boolean
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise
<
void
>
| Promise对象。|
**错误码:**
以下错误码的详细介绍参见
[
分布式数据对象错误码
](
../errorcodes/errorcode-distributed-data_object.md
)
。
| 类型 | 说明 |
| -------- | -------- |
|
boolean | true:标识设置sessionId成功。
<br>
false:标识设置sessionId失败。
|
|
15400001 | 创建内存数据库失败。
|
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
());
// 设置为""退出分布式组网
g_object
.
setSessionId
(
""
);
g_object
.
setSessionId
(
distributedObject
.
genSessionId
()).
then
(()
=>
{
console
.
log
(
"
join session.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
// 退出分布式组网
g_object
.
setSessionId
().
then
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
()).
then
(()
=>
{
console
.
info
(
"
join session.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
// 退出分布式组网
g_object
.
setSessionId
().
then
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
```
### on('change')
### on('change')
<sup>9+</sup>
on(type: 'change', callback: Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
): void
监听分布式数据对象的变更。
监听分布式数据对象的
数据
变更。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -140,9 +334,14 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
changeCallback
=
(
sessionId
,
changeData
)
=>
{
console
.
info
(
"
change
"
+
sessionId
);
if
(
changeData
!=
null
&&
changeData
!=
undefined
)
{
...
...
@@ -154,13 +353,37 @@ globalThis.changeCallback = (sessionId, changeData) => {
g_object
.
on
(
"
change
"
,
globalThis
.
changeCallback
);
```
### off('change')
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
changeCallback
=
(
sessionId
,
changeData
)
=>
{
console
.
info
(
"
change
"
+
sessionId
);
if
(
changeData
!=
null
&&
changeData
!=
undefined
)
{
changeData
.
forEach
(
element
=>
{
console
.
info
(
"
changed !
"
+
element
+
"
"
+
g_object
[
element
]);
});
}
}
g_object
.
on
(
"
change
"
,
globalThis
.
changeCallback
);
```
### off('change')<sup>9+</sup>
off(type: 'change', callback?: Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
): void
当不再进行数据变更监听时,使用此接口删除对象的变更监听。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -172,22 +395,46 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
### on('status')
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
### on('status')<sup>9+</sup>
on(type: 'status', callback: Callback
<
{
sessionId:
string
,
networkId:
string
,
status:
'
online
'
|
'
offline
'
}
>
): void
监听分布式数据对象的上下线。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -198,22 +445,46 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
g_object
.
on
(
"
status
"
,
globalThis
.
statusCallback
);
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
var
g_object
=
distributedObject
.
createDistributedObject
(
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
g_object
.
on
(
"
status
"
,
globalThis
.
statusCallback
);
```
### off('status')
### off('status')
<sup>9+</sup>
off(type: 'status', callback?: Callback
<
{
sessionId:
string
,
deviceId:
string
,
status:
'
online
'
|
'
offline
'
}
>
): void
当不再进行对象上下线监听时,使用此接口删除对象的上下线监听。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -225,9 +496,36 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
// 删除上下线回调changeCallback
g_object
.
off
(
"
status
"
,
globalThis
.
statusCallback
);
// 删除所有的上下线回调
g_object
.
off
(
"
status
"
);
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
...
...
@@ -251,7 +549,7 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void
-
应用卸载。
-
成功恢复数据之后。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -264,7 +562,31 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
,
(
status
,
result
)
=>
{
console
.
log
(
"
save status =
"
+
status
);
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId:
"
+
result
.
sessionId
);
console
.
info
(
"
save version:
"
+
result
.
version
);
console
.
info
(
"
save deviceId:
"
+
result
.
deviceId
);
});
```
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
,
(
status
,
result
)
=>
{
console
.
log
(
"
save status =
"
+
status
);
...
...
@@ -289,7 +611,7 @@ save(deviceId: string): Promise<SaveSuccessResponse>
-
应用卸载。
-
成功恢复数据之后。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -307,7 +629,32 @@ save(deviceId: string): Promise<SaveSuccessResponse>
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
},
()
=>
{
console
.
error
(
"
save failed
"
);
});
```
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
console
.
log
(
"
save callback
"
);
...
...
@@ -328,7 +675,7 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void
如果对象保存在本地设备,那么将删除所有受信任设备上所保存的数据。
如果对象保存在其他设备,那么将删除本地设备上的数据。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
...
...
@@ -338,9 +685,38 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
},
()
=>
{
console
.
error
(
"
save failed
"
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
revokeSave
((
result
,
data
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
...
...
@@ -356,7 +732,7 @@ revokeSave(): Promise<RevokeSaveSuccessResponse>
如果对象保存在本地设备,那么将删除所有受信任设备上所保存的数据。
如果对象保存在其他设备,那么将删除本地设备上的数据。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**返回值:**
...
...
@@ -366,9 +742,14 @@ revokeSave(): Promise<RevokeSaveSuccessResponse>
**示例:**
FA模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
var
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
revokeSave
().
then
((
result
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
...
...
@@ -377,3 +758,231 @@ g_object.revokeSave().then((result) => {
console
.
error
(
"
revokeSave failed
"
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
Ability
from
'
@ohos.application.Ability
'
;
// 获取context
let
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
},
()
=>
{
console
.
error
(
"
save failed
"
);
});
```
## distributedObject.createDistributedObject<sup>(deprecated)</sup>
createDistributedObject(source: object): DistributedObject
创建一个分布式数据对象。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用distributedObject.create替代。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | object | 是 | 设置分布式数据对象的属性。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
|
[
DistributedObject
](
#distributedobjectdeprecated
)
| 创建完成的分布式数据对象。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
// 创建对象,对象包含4个属性类型,string,number,boolean和Object
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
```
## DistributedObject<sup>(deprecated)</sup>
表示一个分布式数据对象。
### setSessionId<sup>(deprecated)</sup>
setSessionId(sessionId?: string): boolean
设置同步的sessionId,当可信组网中有多个设备时,多个设备间的对象如果设置为同一个sessionId,就能自动同步。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用[setSessionId](#setsessionid9)替代。
**需要权限:**
ohos.permission.DISTRIBUTED_DATASYNC。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| sessionId | string | 否 | 分布式数据对象在可信组网中的标识ID。如果要退出分布式组网,设置为""或不设置均可。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | true:标识设置sessionId成功。
<br>
false:标识设置sessionId失败。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});;
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
());
// 设置为""退出分布式组网
g_object
.
setSessionId
(
""
);
```
### on('change')<sup>(deprecated)</sup>
on(type: 'change', callback: Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
): void
监听分布式数据对象的变更。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用[on('change')](#onchange9)替代。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 事件类型,固定为'change',表示数据变更。 |
| callback | Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
| 是 | 变更回调对象实例。
<br>
sessionId:标识变更对象的sessionId;
<br>
fields:标识对象变更的属性名。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
changeCallback
=
(
sessionId
,
changeData
)
=>
{
console
.
info
(
"
change
"
+
sessionId
);
if
(
changeData
!=
null
&&
changeData
!=
undefined
)
{
changeData
.
forEach
(
element
=>
{
console
.
info
(
"
changed !
"
+
element
+
"
"
+
g_object
[
element
]);
});
}
}
g_object
.
on
(
"
change
"
,
globalThis
.
changeCallback
);
```
### off('change')<sup>(deprecated)</sup>
off(type: 'change', callback?: Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
): void
当不再进行数据变更监听时,使用此接口删除对象的变更监听。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用[off('change')](#offchange9)替代。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 事件类型,固定为'change',表示数据变更。 |
| callback | Callback
<
{
sessionId:
string
,
fields:
Array
&
lt
;
string
&
gt
;
}
>
| 否 | 需要删除的数据变更回调,若不设置则删除该对象所有的数据变更回调。
<br>
sessionId:标识变更对象的sessionId;
<br>
fields:标识对象变更的属性名。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
### on('status')<sup>(deprecated)</sup>
on(type: 'status', callback: Callback
<
{
sessionId:
string
,
networkId:
string
,
status:
'
online
'
|
'
offline
'
}
>
): void
监听分布式数据对象的上下线。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用[on('status')](#onstatus9)替代。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 事件类型,固定为'status',表示对象上下线。 |
| callback | Callback
<
{
sessionId:
string
,
networkId:
string
,
status:
'
online
'
\|
'
offline
'
}
>
| 是 | 监听上下线回调实例。
<br>
sessionId:标识变更对象的sessionId;
<br>
networkId:标识对象设备,即deviceId;
<br>
status:标识对象为'online'(上线)或'offline'(下线)的状态。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
g_object
.
on
(
"
status
"
,
globalThis
.
statusCallback
);
```
### off('status')<sup>(deprecated)</sup>
off(type: 'status', callback?: Callback
<
{
sessionId:
string
,
deviceId:
string
,
status:
'
online
'
|
'
offline
'
}
>
): void
当不再进行对象上下线监听时,使用此接口删除对象的上下线监听。
> **说明:**
>
> 从 API Version 8 开始支持,从 API Version 9 开始废弃,建议使用[off('status')](#offstatus9)替代。
**系统能力:**
SystemCapability.DistributedDataManager.DataObject.DistributedObject
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 事件类型,固定为'status',表示对象上下线。 |
| callback | Callback
<
{
sessionId:
string
,
deviceId:
string
,
status:
'
online
'
\|
'
offline
'
}
>
| 否 | 需要删除的上下线回调,若不设置则删除该对象所有的上下线回调。
<br>
sessionId:标识变更对象的sessionId;
<br>
deviceId:标识变更对象的deviceId;
<br>
status:标识对象为'online'(上线)或'offline'(下线)的状态。 |
**示例:**
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
}
// 删除上下线回调changeCallback
g_object
.
off
(
"
status
"
,
globalThis
.
statusCallback
);
// 删除所有的上下线回调
g_object
.
off
(
"
status
"
);
```
\ No newline at end of file
zh-cn/application-dev/reference/errorcodes/errorcode-distributed-data_object.md
0 → 100644
浏览文件 @
9c9d8c4e
# 分布式数据对象错误码
## 15400001 创建内存数据库失败
**错误信息**
Create table failed.
**错误描述**
创建内存数据库失败。
**可能原因**
已经存在加入相同session的对象。
**处理步骤**
检查是否已经有加入相同session的对象。
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录