Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
74ec4f8d
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
74ec4f8d
编写于
8月 25, 2023
作者:
L
LiRui
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
示例代码ArkTs 适配
Signed-off-by:
N
LiRui
<
lirui.WX1229126@huawei.com
>
上级
0699fb1d
变更
4
展开全部
显示空白变更内容
内联
并排
Showing
4 changed file
with
488 addition
and
239 deletion
+488
-239
zh-cn/application-dev/database/data-persistence-by-preferences.md
...plication-dev/database/data-persistence-by-preferences.md
+15
-10
zh-cn/application-dev/database/data-sync-of-distributed-data-object.md
...tion-dev/database/data-sync-of-distributed-data-object.md
+70
-31
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
...tion-dev/reference/apis/js-apis-data-distributedobject.md
+181
-59
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+222
-139
未找到文件。
zh-cn/application-dev/database/data-persistence-by-preferences.md
浏览文件 @
74ec4f8d
...
...
@@ -58,11 +58,13 @@
```
js
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
import
window
from
'
@ohos.window
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
try
{
dataPreferences
.
getPreferences
(
this
.
context
,
'
my
store
'
,
(
err
,
p
references
)
=>
{
dataPreferences
.
getPreferences
(
this
.
context
,
'
my
Store
'
,
(
err
:
BusinessError
,
preferences
:
dataPreferences
.
P
references
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to get preferences. Code:
${
err
.
code
}
,message:
${
err
.
message
}
`
);
return
;
...
...
@@ -82,12 +84,13 @@
```
js
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
try
{
dataPreferences
.
getPreferences
(
context
,
'
mystore
'
,
(
err
,
p
references
)
=>
{
dataPreferences
.
getPreferences
(
this
.
context
,
'
myStore
'
,
(
err
:
BusinessError
,
preferences
:
dataPreferences
.
P
references
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to get preferences. Code:
${
err
.
code
}
,message:
${
err
.
message
}
`
);
return
;
...
...
@@ -157,7 +160,7 @@
```
js
try
{
preferences
.
flush
((
err
)
=>
{
preferences
.
flush
((
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to flush. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
return
;
...
...
@@ -174,18 +177,20 @@
应用订阅数据变更需要指定observer作为回调方法。订阅的Key值发生变更后,当执行flush()方法时,observer被触发回调。示例代码如下所示:
```
js
let
observer
=
function
(
key
)
{
console
.
info
(
'
The key
'
+
key
+
'
changed.
'
);
interface
observer
{
key
:
string
}
preferences
.
on
(
'
change
'
,
observer
);
preferences
.
on
(
'
change
'
,
(
key
:
observer
)
=>
{
console
.
info
(
'
The key
'
+
key
+
'
changed.
'
);
});
// 数据产生变更,由'auto'变为'manual'
preferences
.
put
(
'
startup
'
,
'
manual
'
,
(
err
)
=>
{
preferences
.
put
(
'
startup
'
,
'
manual
'
,
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to put the value of 'startup'. Code:
${
err
.
code
}
,message:
${
err
.
message
}
`
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
((
err
)
=>
{
preferences
.
flush
((
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to flush. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
return
;
...
...
@@ -210,7 +215,7 @@
```
js
try
{
dataPreferences
.
deletePreferences
(
this
.
context
,
'
my
store
'
,
(
err
,
val
)
=>
{
dataPreferences
.
deletePreferences
(
this
.
context
,
'
my
Store
'
,
(
err
:
BusinessError
)
=>
{
if
(
err
)
{
console
.
error
(
`Failed to delete preferences. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
return
;
...
...
zh-cn/application-dev/database/data-sync-of-distributed-data-object.md
浏览文件 @
74ec4f8d
...
...
@@ -151,17 +151,26 @@
// 导入模块
import
distributedDataObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
import
window
from
'
@ohos.window
'
;
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
parent
:
{
[
key
:
string
]:
string
},
list
:
{
[
key
:
string
]:
string
}[]
}
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
let
localObject
=
distributedDataObject
.
create
(
this
.
context
,
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
let
source
:
sourceObject
=
{
name
:
'
jack
'
,
age
:
18
,
isVis
:
false
,
parent
:
{
mother
:
'
jack mom
'
,
father
:
'
jack Dad
'
},
list
:
[{
mother
:
'
jack mom
'
},
{
father
:
'
jack Dad
'
}]
});
}
let
localObject
:
distributedDataObject
.
DataObject
=
distributedDataObject
.
create
(
this
.
context
,
source
);
}
}
```
...
...
@@ -175,34 +184,43 @@
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
let
localObject
=
distributedDataObject
.
create
(
context
,
{
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
parent
:
{
[
key
:
string
]:
string
},
list
:
{
[
key
:
string
]:
string
}[]
}
let
source
:
sourceObject
=
{
name
:
'
jack
'
,
age
:
18
,
isVis
:
false
,
parent
:
{
mother
:
'
jack mom
'
,
father
:
'
jack Dad
'
},
list
:
[{
mother
:
'
jack mom
'
},
{
father
:
'
jack Dad
'
}]
});
}
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
let
localObject
:
distributedDataObject
.
DataObject
=
distributedDataObject
.
create
(
context
,
source
);
```
4.
加入同步组网。同步组网中的数据对象分为发起方和被拉起方。
```
js
// 设备1加入sessionId
let
sessionId
=
'
123456
'
;
let
sessionId
:
string
=
'
123456
'
;
localObject
.
setSessionId
(
sessionId
);
// 和设备1协同的设备2加入同一个session
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
let
remote
Object
=
distributedDataObject
.
create
(
this
.
context
,
{
let
remote
Source
:
sourceObject
=
{
name
:
undefined
,
age
:
undefined
,
// undefined表示数据来自对端
isVis
:
true
,
parent
:
undefined
,
list
:
undefined
});
}
let
remoteObject
:
distributedDataObject
.
DataObject
=
distributedDataObject
.
create
(
this
.
context
,
remoteSource
);
// 收到status上线后remoteObject同步数据,即name变成jack,age是18
remoteObject
.
setSessionId
(
sessionId
);
```
...
...
@@ -210,18 +228,19 @@
5.
监听对象数据变更。可监听对端数据的变更,以callback作为变更回调实例。
```
js
function
changeCallback
(
sessionId
,
changeData
)
{
console
.
info
(
`change:
${
sessionId
}
`
);
interface
ChangeCallback
{
sessionId
:
string
,
fields
:
Array
<
string
>
}
if
(
changeData
!==
null
&&
changeData
!==
undefined
)
{
changeData
.
forEach
(
element
=>
{
console
.
info
(
`The element
${
localObject
[
element
]}
changed.`
);
});
localObject
.
on
(
"
change
"
,
(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
`The element
${
localObject
[
changeData
.
fields
[
index
]]}
changed.`
);
}
}
// 发起方要在changeCallback里刷新界面,则需要将正确的this绑定给changeCallback
localObject
.
on
(
"
change
"
,
this
.
changeCallback
.
bind
(
this
));
});
```
6.
修改对象属性,对象属性支持基本类型(数字类型、布尔类型、字符串类型)以及复杂类型(数组、基本类型嵌套等)。
...
...
@@ -256,7 +275,14 @@
```
js
// 删除变更回调changeCallback
localObject
.
off
(
'
change
'
,
this
.
changeCallback
);
localObject
.
off
(
'
change
'
,(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
"
changed !
"
+
changeData
.
fields
[
index
]
+
"
"
+
g_object
[
changeData
.
fields
[
index
]]);
}
}
});
// 删除所有的变更回调
localObject
.
off
(
'
change
'
);
```
...
...
@@ -264,27 +290,32 @@
9.
监听分布式数据对象的上下线。可以监听对端分布式数据对象的上下线。
```
js
function
statusCallback
(
sessionId
,
networkId
,
status
)
{
// 业务处理
interface
onStatusCallback
{
sessionId
:
string
,
networkId
:
string
,
status
:
'
online
'
|
'
offline
'
}
localObject
.
on
(
'
status
'
,
this
.
statusCallback
);
localObject
.
on
(
'
status
'
,
(
statusCallback
:
onStatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
networkId
);
// 业务处理
});
```
10.
保存和撤回已保存的数据对象。
```
js
// 保存数据对象,如果应用退出后组网内设备需要恢复对象数据时调用
localObject
.
save
(
'
local
'
).
then
((
result
)
=>
{
localObject
.
save
(
"
local
"
).
then
((
result
:
distributedDataObject
.
SaveSuccessResponse
)
=>
{
console
.
info
(
`Succeeded in saving. SessionId:
${
result
.
sessionId
}
,version:
${
result
.
version
}
,deviceId:
${
result
.
deviceId
}
`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
error
(
`Failed to save. Code:
${
err
.
code
}
,message:
${
err
.
message
}
`
);
});
// 撤回保存的数据对象
localObject
.
revokeSave
().
then
((
result
)
=>
{
localObject
.
revokeSave
().
then
((
result
:
distributedDataObject
.
RevokeSaveSuccessResponse
)
=>
{
console
.
info
(
`Succeeded in revokeSaving. Session:
${
result
.
sessionId
}
`
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
error
(
`Failed to revokeSave. Code:
${
err
.
code
}
,message:
${
err
.
message
}
`
);
});
```
...
...
@@ -292,8 +323,16 @@
11.
删除监听分布式数据对象的上下线。可以指定删除监听的上下线回调;也可以不指定,这将会删除该分布式数据对象的所有上下线回调。
```
js
interface
offStatusCallback
{
sessionId
:
string
,
deviceId
:
string
,
status
:
'
online
'
|
'
offline
'
}
// 删除上下线回调statusCallback
localObject
.
off
(
'
status
'
,
this
.
statusCallback
);
localObject
.
off
(
'
status
'
,
(
statusCallback
:
offStatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
deviceId
);
// 业务处理
});
// 删除所有的上下线回调
localObject
.
off
(
'
status
'
);
```
...
...
@@ -302,7 +341,7 @@
```
js
localObject
.
setSessionId
(()
=>
{
console
.
info
(
'
leave all l
ession.
'
);
console
.
info
(
'
leave all s
ession.
'
);
});
```
...
...
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
浏览文件 @
74ec4f8d
...
...
@@ -42,10 +42,20 @@ FA模型示例:
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
// 获取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
"
}});
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
:
distributedObject
.
DataObject
=
distributedObject
.
create
(
context
,
source
);
```
Stage模型示例:
...
...
@@ -54,13 +64,23 @@ Stage模型示例:
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
g_object
=
null
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
import
window
from
'
@ohos.window
'
;
let
g_object
:
distributedObject
.
DataObject
=
null
;
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
g_object
=
distributedObject
.
create
(
this
.
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
g_object
=
distributedObject
.
create
(
this
.
context
,
source
);
}
}
```
...
...
@@ -83,7 +103,7 @@ genSessionId(): string
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
let
sessionId
=
distributedObject
.
genSessionId
();
let
sessionId
:
string
=
distributedObject
.
genSessionId
();
```
## SaveSuccessResponse<sup>9+</sup>
...
...
@@ -179,7 +199,7 @@ g_object.setSessionId(distributedObject.genSessionId(), ()=>{
});
// 退出分布式组网
g_object
.
setSessionId
(()
=>
{
console
.
info
(
"
leave all
l
ession.
"
);
console
.
info
(
"
leave all
s
ession.
"
);
});
```
...
...
@@ -219,13 +239,13 @@ setSessionId(sessionId?: string): Promise<void>
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
()).
then
(()
=>
{
console
.
info
(
"
join session.
"
);
}).
catch
((
error
)
=>
{
}).
catch
((
error
:
BusinessError
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
// 退出分布式组网
g_object
.
setSessionId
().
then
(()
=>
{
console
.
info
(
"
leave all
l
ession.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
leave all
s
ession.
"
);
}).
catch
((
error
:
BusinessError
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
```
...
...
@@ -248,15 +268,18 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri
**示例:**
```
js
globalThis
.
changeCallback
=
(
sessionId
,
changeData
)
=>
{
console
.
info
(
"
change
"
+
sessionId
);
if
(
changeData
!=
null
&&
changeData
!=
undefined
)
{
changeData
.
forEach
(
element
=>
{
console
.
info
(
"
changed !
"
+
element
+
"
"
+
g_object
[
element
]);
});
}
interface
ChangeCallback
{
sessionId
:
string
,
fields
:
Array
<
string
>
}
g_object
.
on
(
"
change
"
,
globalThis
.
changeCallback
);
g_object
.
on
(
"
change
"
,
(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
"
changed !
"
+
changeData
.
fields
[
index
]
+
"
"
+
g_object
[
changeData
.
fields
[
index
]]);
}
}
});
```
### off('change')<sup>9+</sup>
...
...
@@ -279,7 +302,14 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st
```
js
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
g_object
.
off
(
"
change
"
,
(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
"
changed !
"
+
changeData
.
fields
[
index
]
+
"
"
+
g_object
[
changeData
.
fields
[
index
]]);
}
}
});
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
...
...
@@ -302,10 +332,15 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st
**示例:**
```
js
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
interface
onStatusCallback
{
sessionId
:
string
,
networkId
:
string
,
status
:
'
online
'
|
'
offline
'
}
g_object
.
on
(
"
status
"
,
globalThis
.
statusCallback
);
g_object
.
on
(
"
status
"
,
(
statusCallback
:
onStatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
networkId
);
});
```
### off('status')<sup>9+</sup>
...
...
@@ -327,11 +362,15 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
**示例:**
```
js
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
interface
offStatusCallback
{
sessionId
:
string
,
networkId
:
string
,
status
:
'
online
'
|
'
offline
'
}
// 删除上下线回调changeCallback
g_object
.
off
(
"
status
"
,
globalThis
.
statusCallback
);
g_object
.
off
(
"
status
"
,
(
statusCallback
:
StatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
networkId
);
});
// 删除所有的上下线回调
g_object
.
off
(
"
status
"
);
```
...
...
@@ -363,7 +402,7 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void
```
ts
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
,
(
err
,
result
)
=>
{
g_object
.
save
(
"
local
"
,
(
err
:
BusinessError
,
result
:
distributedObject
.
SaveSuccessResponse
)
=>
{
if
(
err
)
{
console
.
info
(
"
save failed, error code =
"
+
err
.
code
);
console
.
info
(
"
save failed, error message:
"
+
err
.
message
);
...
...
@@ -408,12 +447,12 @@ save(deviceId: string): Promise<SaveSuccessResponse>
```
js
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
g_object
.
save
(
"
local
"
).
then
((
result
:
distributedObject
.
SaveSuccessResponse
)
=>
{
console
.
info
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
"
save failed, error code =
"
+
err
.
code
);
console
.
info
(
"
save failed, error message:
"
+
err
.
message
);
});
...
...
@@ -441,7 +480,7 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void
```
js
g_object
.
setSessionId
(
"
123456
"
);
// 持久化数据
g_object
.
save
(
"
local
"
,
(
err
,
result
)
=>
{
g_object
.
save
(
"
local
"
,
(
err
:
BusinessError
,
result
:
distributedObject
.
SaveSuccessResponse
)
=>
{
if
(
err
)
{
console
.
info
(
"
save failed, error code =
"
+
err
.
code
);
console
.
info
(
"
save failed, error message:
"
+
err
.
message
);
...
...
@@ -453,7 +492,7 @@ g_object.save("local", (err, result) => {
console
.
info
(
"
save deviceId:
"
+
result
.
deviceId
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
((
err
,
result
)
=>
{
g_object
.
revokeSave
((
err
:
BusinessError
,
result
:
distributedObject
.
RevokeSaveSuccessResponse
)
=>
{
if
(
err
)
{
console
.
info
(
"
revokeSave failed, error code =
"
+
err
.
code
);
console
.
info
(
"
revokeSave failed, error message:
"
+
err
.
message
);
...
...
@@ -486,20 +525,20 @@ revokeSave(): Promise<RevokeSaveSuccessResponse>
```
ts
g_object
.
setSessionId
(
"
123456
"
);
// 持久化数据
g_object
.
save
(
"
local
"
).
then
((
result
)
=>
{
g_object
.
save
(
"
local
"
).
then
((
result
:
distributedObject
.
SaveSuccessResponse
)
=>
{
console
.
info
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
"
save failed, error code =
"
+
err
.
code
);
console
.
info
(
"
save failed, error message:
"
+
err
.
message
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
().
then
((
result
)
=>
{
g_object
.
revokeSave
().
then
((
result
:
distributedObject
.
RevokeSaveSuccessResponse
)
=>
{
console
.
info
(
"
revokeSave callback
"
);
console
.
info
(
"
sessionId
"
+
result
.
sessionId
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
:
BusinessError
)
=>
{
console
.
info
(
"
revokeSave failed, error code =
"
+
err
.
code
);
console
.
info
(
"
revokeSave failed, error message =
"
+
err
.
message
);
});
...
...
@@ -534,8 +573,17 @@ createDistributedObject(source: object): DistributedObject
```
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
"
}});
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
```
## DistributedObject<sup>(deprecated)</sup>
...
...
@@ -572,7 +620,17 @@ setSessionId(sessionId?: string): boolean
```
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
"
}});;
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
// g_object加入分布式组网
g_object
.
setSessionId
(
distributedObject
.
genSessionId
());
// 设置为""退出分布式组网
...
...
@@ -602,16 +660,29 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri
```
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
]);
});
}
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
interface
ChangeCallback
{
sessionId
:
string
,
fields
:
Array
<
string
>
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
g_object
.
on
(
"
change
"
,
globalThis
.
changeCallback
);
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
g_object
.
on
(
"
change
"
,
(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
"
changed !
"
+
changeData
.
fields
[
index
]
+
"
"
+
g_object
[
changeData
.
fields
[
index
]]);
}
}
});
```
### off('change')<sup>(deprecated)</sup>
...
...
@@ -638,9 +709,30 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st
```
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
"
}});
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
interface
ChangeCallback
{
sessionId
:
string
,
fields
:
Array
<
string
>
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
g_object
.
off
(
"
change
"
,
(
changeData
:
ChangeCallback
)
=>
{
console
.
info
(
"
change
"
+
changeData
.
sessionId
);
if
(
changeData
.
fields
!=
null
&&
changeData
.
fields
!=
undefined
)
{
for
(
let
index
:
number
=
0
;
index
<
changeData
.
fields
.
length
;
index
++
)
{
console
.
info
(
"
changed !
"
+
changeData
.
fields
[
index
]
+
"
"
+
g_object
[
changeData
.
fields
[
index
]]);
}
}
});
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
...
...
@@ -668,11 +760,27 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
globalThis
.
statusCallback
=
(
sessionId
,
networkId
,
status
)
=>
{
globalThis
.
response
+=
"
status changed
"
+
sessionId
+
"
"
+
status
+
"
"
+
networkId
;
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
interface
StatusCallback
{
sessionId
:
string
,
networkId
:
string
,
status
:
'
online
'
|
'
offline
'
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
=
distributedObject
.
createDistributedObject
({
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
g_object
.
on
(
"
status
"
,
globalThis
.
statusCallback
);
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
g_object
.
on
(
"
status
"
,
(
statusCallback
:
StatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
networkId
);
});
```
### off('status')<sup>(deprecated)</sup>
...
...
@@ -699,12 +807,26 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
```
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
;
interface
sourceObject
{
name
:
string
,
age
:
number
,
isVis
:
boolean
}
interface
offStatusCallback
{
sessionId
:
string
,
deviceId
:
string
,
status
:
'
online
'
|
'
offline
'
}
let
source
:
sourceObject
=
{
name
:
"
amy
"
,
age
:
18
,
isVis
:
false
}
let
g_object
:
distributedObject
.
DistributedObject
=
distributedObject
.
createDistributedObject
(
source
);
// 删除上下线回调changeCallback
g_object
.
off
(
"
status
"
,
globalThis
.
statusCallback
);
g_object
.
off
(
"
status
"
,
(
statusCallback
:
offStatusCallback
)
=>
{
console
.
info
(
"
status changed
"
+
statusCallback
.
sessionId
+
"
"
+
statusCallback
.
status
+
"
"
+
statusCallback
.
deviceId
);
});
// 删除所有的上下线回调
g_object
.
off
(
"
status
"
);
```
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
74ec4f8d
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录