Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
360f6f8b
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,发现更多精彩内容 >>
未验证
提交
360f6f8b
编写于
3月 01, 2023
作者:
O
openharmony_ci
提交者:
Gitee
3月 01, 2023
浏览文件
操作
浏览文件
下载
差异文件
!15251 preferences&object示例代码规范化
Merge pull request !15251 from wangxiyue/up_OpenHarmony-3.2-Beta5
上级
860be038
b2c90328
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
110 addition
and
459 deletion
+110
-459
zh-cn/application-dev/database/database-distributedobject-guidelines.md
...ion-dev/database/database-distributedobject-guidelines.md
+1
-1
zh-cn/application-dev/database/database-preference-guidelines.md
...pplication-dev/database/database-preference-guidelines.md
+8
-10
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
...tion-dev/reference/apis/js-apis-data-distributedobject.md
+20
-350
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+81
-98
未找到文件。
zh-cn/application-dev/database/database-distributedobject-guidelines.md
浏览文件 @
360f6f8b
...
...
@@ -142,7 +142,7 @@
function
grantPermission
()
{
let
permissions
=
[
'
ohos.permission.DISTRIBUTED_DATASYNC
'
];
context
.
requestPermissionsFromUser
(
permissions
).
then
((
data
)
=>
{
console
.
log
(
'
success: ${data}
'
);
console
.
info
(
'
success: ${data}
'
);
}).
catch
((
error
)
=>
{
console
.
error
(
'
failed: ${error}
'
);
});
...
...
zh-cn/application-dev/database/database-preference-guidelines.md
浏览文件 @
360f6f8b
...
...
@@ -113,22 +113,20 @@
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
let
context
=
null
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
preferences
=
null
;
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
onWindowStageCreate
(
windowStage
)
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
);
promise
.
then
((
pref
)
=>
{
preferences
=
pref
;
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to get preferences.
"
);
})
}
}
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
);
promise
.
then
((
pref
)
=>
{
preferences
=
pref
;
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to get preferences.
"
);
})
```
3.
存入数据。
...
...
zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md
浏览文件 @
360f6f8b
...
...
@@ -36,15 +36,13 @@ create(context: Context, source: object): DistributedObjectV9
**示例:**
FA模型示例:
```
js
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
// 获取context
let
context
=
featureAbility
.
getContext
();
// 创建对象,该对象包含4个属性类型
,string,number,
boolean和Object
// 创建对象,该对象包含4个属性类型
:string、number、
boolean和Object
let
g_object
=
distributedObject
.
create
(
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
```
...
...
@@ -54,15 +52,15 @@ Stage模型示例:
// 导入模块
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
let
g_object
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object
g_object
=
distributedObject
.
create
(
this
.
context
,
{
name
:
"
Amy
"
,
age
:
18
,
isVis
:
false
,
parent
:{
mother
:
"
jack mom
"
,
father
:
"
jack Dad
"
}});
}
}
// 创建对象,该对象包含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
...
...
@@ -110,7 +108,7 @@ revokeSave接口回调信息。
## DistributedObjectV9
表示一个分布式数据对象。
表示一个分布式数据对象。
在使用以下接口前,需调用
[
create()
](
#distributedobjectcreate9
)
获取DistributedObjectV9对象。
### setSessionId<sup>9+</sup>
...
...
@@ -139,35 +137,10 @@ setSessionId(sessionId: string, callback: AsyncCallback<void>): void
**示例:**
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
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
"
);
console
.
info
(
"
join session
"
);
});
```
...
...
@@ -197,43 +170,14 @@ setSessionId(callback: AsyncCallback<void>): void
**示例:**
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
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
"
);
console
.
info
(
"
join session
"
);
});
// 退出分布式组网
g_object
.
setSessionId
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
console
.
info
(
"
leave all lession.
"
);
});
```
...
...
@@ -269,40 +213,7 @@ setSessionId(sessionId?: string): Promise<void>
**示例:**
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
()).
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
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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.
"
);
...
...
@@ -311,7 +222,7 @@ g_object.setSessionId(distributedObject.genSessionId()).then (()=>{
});
// 退出分布式组网
g_object
.
setSessionId
().
then
(()
=>
{
console
.
log
(
"
leave all lession.
"
);
console
.
info
(
"
leave all lession.
"
);
}).
catch
((
error
)
=>
{
console
.
info
(
"
error:
"
+
error
.
code
+
error
.
message
);
});
...
...
@@ -334,38 +245,7 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri
**示例:**
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
"
}});
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
);
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
)
{
...
...
@@ -395,33 +275,7 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st
**示例:**
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
"
}});
// 删除数据变更回调changeCallback
g_object
.
off
(
"
change
"
,
globalThis
.
changeCallback
);
// 删除所有的数据变更回调
g_object
.
off
(
"
change
"
);
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
);
// 删除所有的数据变更回调
...
...
@@ -445,36 +299,10 @@ 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
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
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
);
```
...
...
@@ -496,36 +324,7 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
**示例:**
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
"
}});
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
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
;
}
...
...
@@ -560,37 +359,10 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void
**示例:**
FA模型示例
```
ts
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
});
g_object
.
setSessionId
(
"
123456
"
);
g_object
.
save
(
"
local
"
,
(
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId:
"
+
result
.
sessionId
);
console
.
info
(
"
save version:
"
+
result
.
version
);
console
.
info
(
"
save deviceId:
"
+
result
.
deviceId
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
"
,
(
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save callback
"
);
console
.
info
(
"
save sessionId:
"
+
result
.
sessionId
);
console
.
info
(
"
save version:
"
+
result
.
version
);
console
.
info
(
"
save deviceId:
"
+
result
.
deviceId
);
...
...
@@ -627,40 +399,10 @@ save(deviceId: string): Promise<SaveSuccessResponse>
**示例:**
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
});
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模型示例
```
js
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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 callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
...
...
@@ -688,54 +430,19 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void
**示例:**
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
});
g_object
.
setSessionId
(
"
123456
"
);
// 持久化数据
g_object
.
save
(
"
local
"
,
(
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
((
result
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
console
.
log
(
"
revokeSave sessionId
"
+
result
.
sessionId
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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
"
,
(
result
)
=>
{
console
.
log
(
"
save callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
((
result
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
console
.
log
(
"
revokeSave sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
revokeSave callback
"
);
console
.
info
(
"
revokeSave sessionId
"
+
result
.
sessionId
);
});
```
...
...
@@ -761,57 +468,20 @@ revokeSave(): Promise<RevokeSaveSuccessResponse>
FA模型示例
```
ts
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
});
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
"
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
().
then
((
result
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
console
.
log
(
"
sessionId
"
+
result
.
sessionId
);
},
()
=>
{
console
.
error
(
"
revokeSave failed
"
);
});
```
Stage模型示例
```
ts
import
distributedObject
from
'
@ohos.data.distributedDataObject
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
// 获取context
let
context
;
class
EntryAbility
extends
UIAbility
{
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 callback
"
);
console
.
info
(
"
save sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
save version
"
+
result
.
version
);
console
.
info
(
"
save deviceId
"
+
result
.
deviceId
);
},
()
=>
{
console
.
error
(
"
save failed
"
);
});
// 删除持久化保存的数据
g_object
.
revokeSave
().
then
((
result
)
=>
{
console
.
log
(
"
revokeSave callback
"
);
console
.
log
(
"
sessionId
"
+
result
.
sessionId
);
console
.
info
(
"
revokeSave callback
"
);
console
.
info
(
"
sessionId
"
+
result
.
sessionId
);
},
()
=>
{
console
.
error
(
"
revokeSave failed
"
);
});
...
...
@@ -852,7 +522,7 @@ let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, is
## DistributedObject<sup>(deprecated)</sup>
表示一个分布式数据对象。
表示一个分布式数据对象。
在使用以下接口前,需调用
[
createDistributedObject()
](
#distributedobjectcreatedistributedobjectdeprecated
)
获取DistributedObject对象。
### setSessionId<sup>(deprecated)</sup>
...
...
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
360f6f8b
...
...
@@ -58,6 +58,7 @@ try {
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
...
...
@@ -68,26 +69,25 @@ try {
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
}
}
let
preferences
=
null
;
try
{
data_preferences
.
getPreferences
(
context
,
'
mystore
'
,
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
getPreferences
(
this
.
context
,
'
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
);
}
}
}
```
...
...
@@ -128,36 +128,34 @@ try {
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
log
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
}
}
let
preferences
=
null
;
try
{
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
);
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
log
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
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
);
}
}
}
```
...
...
@@ -214,25 +212,21 @@ try {
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
}
}
try
{
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
,
function
(
err
,
val
)
{
if
(
err
)
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
val
)
{
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
);
return
;
}
console
.
info
(
"
Succeeded in deleting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
```
...
...
@@ -293,25 +287,21 @@ try {
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
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
);
}
}
}
try
{
let
promise
=
data_preferences
.
deletePreferences
(
context
,
'
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
);
}
```
## data_preferences.removePreferencesFromCache
...
...
@@ -357,25 +347,21 @@ try {
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
}
}
try
{
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
,
function
(
err
,
val
)
{
if
(
err
)
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
,
val
)
{
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
);
return
;
}
console
.
info
(
"
Succeeded in removing preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
}
```
...
...
@@ -427,25 +413,22 @@ try {
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
context
=
null
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
;
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
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
);
}
}
}
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
'
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
);
}
```
## Preferences
...
...
@@ -794,10 +777,10 @@ try {
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting the key 'startup'.
"
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
Failed to delete the key 'startup'. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to delete the key 'startup'. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
}
catch
(
err
)
{
console
.
log
(
"
Failed to delete the key 'startup'. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to delete the key 'startup'. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录