提交 74ec4f8d 编写于 作者: L LiRui

示例代码ArkTs 适配

Signed-off-by: NLiRui <lirui.WX1229126@huawei.com>
上级 0699fb1d
...@@ -58,11 +58,13 @@ ...@@ -58,11 +58,13 @@
```js ```js
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
dataPreferences.getPreferences(this.context, 'mystore', (err, preferences) => { dataPreferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: dataPreferences.Preferences) => {
if (err) { if (err) {
console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`); console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
return; return;
...@@ -82,12 +84,13 @@ ...@@ -82,12 +84,13 @@
```js ```js
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base';
// 获取context // 获取context
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
dataPreferences.getPreferences(context, 'mystore', (err, preferences) => { dataPreferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: dataPreferences.Preferences) => {
if (err) { if (err) {
console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`); console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
return; return;
...@@ -157,7 +160,7 @@ ...@@ -157,7 +160,7 @@
```js ```js
try { try {
preferences.flush((err) => { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);
return; return;
...@@ -174,18 +177,20 @@ ...@@ -174,18 +177,20 @@
应用订阅数据变更需要指定observer作为回调方法。订阅的Key值发生变更后,当执行flush()方法时,observer被触发回调。示例代码如下所示: 应用订阅数据变更需要指定observer作为回调方法。订阅的Key值发生变更后,当执行flush()方法时,observer被触发回调。示例代码如下所示:
```js ```js
let observer = function (key) { interface observer {
console.info('The key' + key + 'changed.'); key: string
} }
preferences.on('change', observer); preferences.on('change', (key: observer) => {
console.info('The key' + key + 'changed.');
});
// 数据产生变更,由'auto'变为'manual' // 数据产生变更,由'auto'变为'manual'
preferences.put('startup', 'manual', (err) => { preferences.put('startup', 'manual', (err: BusinessError) => {
if (err) { if (err) {
console.error(`Failed to put the value of 'startup'. Code:${err.code},message:${err.message}`); console.error(`Failed to put the value of 'startup'. Code:${err.code},message:${err.message}`);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush((err) => { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);
return; return;
...@@ -210,7 +215,7 @@ ...@@ -210,7 +215,7 @@
```js ```js
try { try {
dataPreferences.deletePreferences(this.context, 'mystore', (err, val) => { dataPreferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => {
if (err) { if (err) {
console.error(`Failed to delete preferences. Code:${err.code}, message:${err.message}`); console.error(`Failed to delete preferences. Code:${err.code}, message:${err.message}`);
return; return;
......
...@@ -151,17 +151,26 @@ ...@@ -151,17 +151,26 @@
// 导入模块 // 导入模块
import distributedDataObject from '@ohos.data.distributedDataObject'; import distributedDataObject from '@ohos.data.distributedDataObject';
import UIAbility from '@ohos.app.ability.UIAbility'; 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 { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object let source: sourceObject = {
let localObject = distributedDataObject.create(this.context, {
name: 'jack', name: 'jack',
age: 18, age: 18,
isVis: false, isVis: false,
parent: { mother: 'jack mom', father: 'jack Dad' }, parent: { mother: 'jack mom', father: 'jack Dad' },
list: [{ 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 @@ ...@@ -175,34 +184,43 @@
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
// 获取context // 获取context
let context = featureAbility.getContext(); let context = featureAbility.getContext();
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object interface sourceObject{
let localObject = distributedDataObject.create(context, { name: string,
age: number,
isVis: boolean
parent: { [key: string]: string },
list: { [key: string]: string }[]
}
let source: sourceObject = {
name: 'jack', name: 'jack',
age: 18, age: 18,
isVis: false, isVis: false,
parent: { mother: 'jack mom', father: 'jack Dad' }, parent: { mother: 'jack mom', father: 'jack Dad' },
list: [{ 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. 加入同步组网。同步组网中的数据对象分为发起方和被拉起方。 4. 加入同步组网。同步组网中的数据对象分为发起方和被拉起方。
```js ```js
// 设备1加入sessionId // 设备1加入sessionId
let sessionId = '123456'; let sessionId: string = '123456';
localObject.setSessionId(sessionId); localObject.setSessionId(sessionId);
// 和设备1协同的设备2加入同一个session // 和设备1协同的设备2加入同一个session
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object // 创建对象,该对象包含4个属性类型:string、number、boolean和Object
let remoteObject = distributedDataObject.create(this.context, { let remoteSource: sourceObject = {
name: undefined, name: undefined,
age: undefined, // undefined表示数据来自对端 age: undefined, // undefined表示数据来自对端
isVis: true, isVis: true,
parent: undefined, parent: undefined,
list: undefined list: undefined
}); }
let remoteObject: distributedDataObject.DataObject = distributedDataObject.create(this.context, remoteSource);
// 收到status上线后remoteObject同步数据,即name变成jack,age是18 // 收到status上线后remoteObject同步数据,即name变成jack,age是18
remoteObject.setSessionId(sessionId); remoteObject.setSessionId(sessionId);
``` ```
...@@ -210,18 +228,19 @@ ...@@ -210,18 +228,19 @@
5. 监听对象数据变更。可监听对端数据的变更,以callback作为变更回调实例。 5. 监听对象数据变更。可监听对端数据的变更,以callback作为变更回调实例。
```js ```js
function changeCallback(sessionId, changeData) { interface ChangeCallback {
console.info(`change: ${sessionId}`); sessionId: string,
fields: Array<string>
}
if (changeData !== null && changeData !== undefined) { localObject.on("change", (changeData:ChangeCallback) => {
changeData.forEach(element => { console.info("change" + changeData.sessionId);
console.info(`The element ${localObject[element]} changed.`); 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. 修改对象属性,对象属性支持基本类型(数字类型、布尔类型、字符串类型)以及复杂类型(数组、基本类型嵌套等)。 6. 修改对象属性,对象属性支持基本类型(数字类型、布尔类型、字符串类型)以及复杂类型(数组、基本类型嵌套等)。
...@@ -256,7 +275,14 @@ ...@@ -256,7 +275,14 @@
```js ```js
// 删除变更回调changeCallback // 删除变更回调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'); localObject.off('change');
``` ```
...@@ -264,27 +290,32 @@ ...@@ -264,27 +290,32 @@
9. 监听分布式数据对象的上下线。可以监听对端分布式数据对象的上下线。 9. 监听分布式数据对象的上下线。可以监听对端分布式数据对象的上下线。
```js ```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. 保存和撤回已保存的数据对象。 10. 保存和撤回已保存的数据对象。
```js ```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}`); 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}`); 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}`); console.info(`Succeeded in revokeSaving. Session:${result.sessionId}`);
}).catch((err) => { }).catch((err: BusinessError) => {
console.error(`Failed to revokeSave. Code:${err.code},message:${err.message}`); console.error(`Failed to revokeSave. Code:${err.code},message:${err.message}`);
}); });
``` ```
...@@ -292,8 +323,16 @@ ...@@ -292,8 +323,16 @@
11. 删除监听分布式数据对象的上下线。可以指定删除监听的上下线回调;也可以不指定,这将会删除该分布式数据对象的所有上下线回调。 11. 删除监听分布式数据对象的上下线。可以指定删除监听的上下线回调;也可以不指定,这将会删除该分布式数据对象的所有上下线回调。
```js ```js
interface offStatusCallback {
sessionId: string,
deviceId: string,
status: 'online' | 'offline'
}
// 删除上下线回调statusCallback // 删除上下线回调statusCallback
localObject.off('status', this.statusCallback); localObject.off('status', (statusCallback: offStatusCallback) => {
console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.deviceId);
// 业务处理
});
// 删除所有的上下线回调 // 删除所有的上下线回调
localObject.off('status'); localObject.off('status');
``` ```
...@@ -302,7 +341,7 @@ ...@@ -302,7 +341,7 @@
```js ```js
localObject.setSessionId(() => { localObject.setSessionId(() => {
console.info('leave all lession.'); console.info('leave all session.');
}); });
``` ```
......
...@@ -42,10 +42,20 @@ FA模型示例: ...@@ -42,10 +42,20 @@ FA模型示例:
// 导入模块 // 导入模块
import distributedObject from '@ohos.data.distributedDataObject'; import distributedObject from '@ohos.data.distributedDataObject';
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base';
// 获取context // 获取context
let context = featureAbility.getContext(); let context = featureAbility.getContext();
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object interface sourceObject{
let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); 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模型示例: Stage模型示例:
...@@ -54,13 +64,23 @@ Stage模型示例: ...@@ -54,13 +64,23 @@ Stage模型示例:
// 导入模块 // 导入模块
import distributedObject from '@ohos.data.distributedDataObject'; import distributedObject from '@ohos.data.distributedDataObject';
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
let g_object = null; import window from '@ohos.window';
let g_object: distributedObject.DataObject = null;
interface sourceObject{
name: string,
age: number,
isVis: boolean
}
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage){ onWindowStageCreate(windowStage: window.WindowStage) {
// 创建对象,该对象包含4个属性类型:string、number、boolean和Object let source: sourceObject = {
g_object = distributedObject.create(this.context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); name: "amy",
age:18,
isVis:false
}
g_object = distributedObject.create(this.context, source);
} }
} }
``` ```
...@@ -83,7 +103,7 @@ genSessionId(): string ...@@ -83,7 +103,7 @@ genSessionId(): string
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; import distributedObject from '@ohos.data.distributedDataObject';
let sessionId = distributedObject.genSessionId(); let sessionId: string = distributedObject.genSessionId();
``` ```
## SaveSuccessResponse<sup>9+</sup> ## SaveSuccessResponse<sup>9+</sup>
...@@ -179,7 +199,7 @@ g_object.setSessionId(distributedObject.genSessionId(), ()=>{ ...@@ -179,7 +199,7 @@ g_object.setSessionId(distributedObject.genSessionId(), ()=>{
}); });
// 退出分布式组网 // 退出分布式组网
g_object.setSessionId(() => { g_object.setSessionId(() => {
console.info("leave all lession."); console.info("leave all session.");
}); });
``` ```
...@@ -219,13 +239,13 @@ setSessionId(sessionId?: string): Promise&lt;void&gt; ...@@ -219,13 +239,13 @@ setSessionId(sessionId?: string): Promise&lt;void&gt;
// g_object加入分布式组网 // g_object加入分布式组网
g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ g_object.setSessionId(distributedObject.genSessionId()).then (()=>{
console.info("join session."); console.info("join session.");
}).catch((error)=>{ }).catch((error: BusinessError)=>{
console.info("error:" + error.code + error.message); console.info("error:" + error.code + error.message);
}); });
// 退出分布式组网 // 退出分布式组网
g_object.setSessionId().then (()=>{ g_object.setSessionId().then (()=>{
console.info("leave all lession."); console.info("leave all session.");
}).catch((error)=>{ }).catch((error: BusinessError)=>{
console.info("error:" + error.code + error.message); console.info("error:" + error.code + error.message);
}); });
``` ```
...@@ -248,15 +268,18 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;stri ...@@ -248,15 +268,18 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;stri
**示例:** **示例:**
```js ```js
globalThis.changeCallback = (sessionId, changeData) => { interface ChangeCallback {
console.info("change" + sessionId); sessionId: string,
if (changeData != null && changeData != undefined) { fields: Array<string>
changeData.forEach(element => {
console.info("changed !" + element + " " + g_object[element]);
});
}
} }
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> ### off('change')<sup>9+</sup>
...@@ -279,7 +302,14 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;st ...@@ -279,7 +302,14 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;st
```js ```js
// 删除数据变更回调changeCallback // 删除数据变更回调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"); g_object.off("change");
``` ```
...@@ -302,10 +332,15 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st ...@@ -302,10 +332,15 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st
**示例:** **示例:**
```js ```js
globalThis.statusCallback = (sessionId, networkId, status) => { interface onStatusCallback {
globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; 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> ### off('status')<sup>9+</sup>
...@@ -327,11 +362,15 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s ...@@ -327,11 +362,15 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
**示例:** **示例:**
```js ```js
globalThis.statusCallback = (sessionId, networkId, status) => { interface offStatusCallback {
globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; sessionId: string,
networkId: string,
status: 'online' | 'offline'
} }
// 删除上下线回调changeCallback // 删除上下线回调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"); g_object.off("status");
``` ```
...@@ -363,7 +402,7 @@ save(deviceId: string, callback: AsyncCallback&lt;SaveSuccessResponse&gt;): void ...@@ -363,7 +402,7 @@ save(deviceId: string, callback: AsyncCallback&lt;SaveSuccessResponse&gt;): void
```ts ```ts
g_object.setSessionId("123456"); g_object.setSessionId("123456");
g_object.save("local", (err, result) => { g_object.save("local", (err: BusinessError, result:distributedObject.SaveSuccessResponse) => {
if (err) { if (err) {
console.info("save failed, error code = " + err.code); console.info("save failed, error code = " + err.code);
console.info("save failed, error message: " + err.message); console.info("save failed, error message: " + err.message);
...@@ -408,12 +447,12 @@ save(deviceId: string): Promise&lt;SaveSuccessResponse&gt; ...@@ -408,12 +447,12 @@ save(deviceId: string): Promise&lt;SaveSuccessResponse&gt;
```js ```js
g_object.setSessionId("123456"); 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 callback");
console.info("save sessionId " + result.sessionId); console.info("save sessionId " + result.sessionId);
console.info("save version " + result.version); console.info("save version " + result.version);
console.info("save deviceId " + result.deviceId); console.info("save deviceId " + result.deviceId);
}).catch((err) => { }).catch((err: BusinessError) => {
console.info("save failed, error code = " + err.code); console.info("save failed, error code = " + err.code);
console.info("save failed, error message: " + err.message); console.info("save failed, error message: " + err.message);
}); });
...@@ -441,7 +480,7 @@ revokeSave(callback: AsyncCallback&lt;RevokeSaveSuccessResponse&gt;): void ...@@ -441,7 +480,7 @@ revokeSave(callback: AsyncCallback&lt;RevokeSaveSuccessResponse&gt;): void
```js ```js
g_object.setSessionId("123456"); g_object.setSessionId("123456");
// 持久化数据 // 持久化数据
g_object.save("local", (err, result) => { g_object.save("local", (err: BusinessError, result: distributedObject.SaveSuccessResponse) => {
if (err) { if (err) {
console.info("save failed, error code = " + err.code); console.info("save failed, error code = " + err.code);
console.info("save failed, error message: " + err.message); console.info("save failed, error message: " + err.message);
...@@ -453,7 +492,7 @@ g_object.save("local", (err, result) => { ...@@ -453,7 +492,7 @@ g_object.save("local", (err, result) => {
console.info("save deviceId: " + result.deviceId); console.info("save deviceId: " + result.deviceId);
}); });
// 删除持久化保存的数据 // 删除持久化保存的数据
g_object.revokeSave((err, result) => { g_object.revokeSave((err: BusinessError, result: distributedObject.RevokeSaveSuccessResponse) => {
if (err) { if (err) {
console.info("revokeSave failed, error code = " + err.code); console.info("revokeSave failed, error code = " + err.code);
console.info("revokeSave failed, error message: " + err.message); console.info("revokeSave failed, error message: " + err.message);
...@@ -486,20 +525,20 @@ revokeSave(): Promise&lt;RevokeSaveSuccessResponse&gt; ...@@ -486,20 +525,20 @@ revokeSave(): Promise&lt;RevokeSaveSuccessResponse&gt;
```ts ```ts
g_object.setSessionId("123456"); 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 callback");
console.info("save sessionId " + result.sessionId); console.info("save sessionId " + result.sessionId);
console.info("save version " + result.version); console.info("save version " + result.version);
console.info("save deviceId " + result.deviceId); console.info("save deviceId " + result.deviceId);
}).catch((err) => { }).catch((err: BusinessError) => {
console.info("save failed, error code = " + err.code); console.info("save failed, error code = " + err.code);
console.info("save failed, error message: " + err.message); 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("revokeSave callback");
console.info("sessionId" + result.sessionId); console.info("sessionId" + result.sessionId);
}).catch((err)=> { }).catch((err: BusinessError)=> {
console.info("revokeSave failed, error code = " + err.code); console.info("revokeSave failed, error code = " + err.code);
console.info("revokeSave failed, error message = " + err.message); console.info("revokeSave failed, error message = " + err.message);
}); });
...@@ -534,8 +573,17 @@ createDistributedObject(source: object): DistributedObject ...@@ -534,8 +573,17 @@ createDistributedObject(source: object): DistributedObject
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; import distributedObject from '@ohos.data.distributedDataObject';
// 创建对象,对象包含4个属性类型,string,number,boolean和Object interface sourceObject{
let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); 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> ## DistributedObject<sup>(deprecated)</sup>
...@@ -572,7 +620,17 @@ setSessionId(sessionId?: string): boolean ...@@ -572,7 +620,17 @@ setSessionId(sessionId?: string): boolean
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; 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加入分布式组网
g_object.setSessionId(distributedObject.genSessionId()); g_object.setSessionId(distributedObject.genSessionId());
// 设置为""退出分布式组网 // 设置为""退出分布式组网
...@@ -602,16 +660,29 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;stri ...@@ -602,16 +660,29 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;stri
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; 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{
globalThis.changeCallback = (sessionId, changeData) => { name: string,
console.info("change" + sessionId); age: number,
if (changeData != null && changeData != undefined) { isVis: boolean
changeData.forEach(element => { }
console.info("changed !" + element + " " + g_object[element]); 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> ### off('change')<sup>(deprecated)</sup>
...@@ -638,9 +709,30 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;st ...@@ -638,9 +709,30 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;st
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; 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 // 删除数据变更回调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"); g_object.off("change");
``` ```
...@@ -668,11 +760,27 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st ...@@ -668,11 +760,27 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; 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"}}); let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
g_object.on("status", globalThis.statusCallback);
g_object.on("status", (statusCallback:StatusCallback) => {
console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId);
});
``` ```
### off('status')<sup>(deprecated)</sup> ### off('status')<sup>(deprecated)</sup>
...@@ -699,12 +807,26 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s ...@@ -699,12 +807,26 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; 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{
globalThis.statusCallback = (sessionId, networkId, status) => { name: string,
globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; 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 // 删除上下线回调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"); g_object.off("status");
``` ```
...@@ -49,11 +49,13 @@ FA模型示例: ...@@ -49,11 +49,13 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base';
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let preferences = null; let preferences: data_preferences.Preferences = null;
try { try {
data_preferences.getPreferences(context, 'mystore', function (err, val) { data_preferences.getPreferences(context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -70,13 +72,15 @@ Stage模型示例: ...@@ -70,13 +72,15 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';
let preferences = null; let preferences: data_preferences.Preferences = null;
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.getPreferences(this.context, 'mystore', function (err, val) { data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -119,15 +123,17 @@ FA模型示例: ...@@ -119,15 +123,17 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let preferences = null; let preferences: data_preferences.Preferences = null;
try { try {
let promise = data_preferences.getPreferences(context, 'mystore'); let promise = data_preferences.getPreferences(context, 'myStore');
promise.then((object) => { promise.then((object: data_preferences.Preferences) => {
preferences = object; preferences = object;
console.info("Succeeded in getting preferences."); console.info("Succeeded in getting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -139,17 +145,19 @@ Stage模型示例: ...@@ -139,17 +145,19 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
let preferences = null; let preferences: data_preferences.Preferences = null;
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
let promise = data_preferences.getPreferences(this.context, 'mystore'); let promise = data_preferences.getPreferences(this.context, 'myStore');
promise.then((object) => { promise.then((object: data_preferences.Preferences) => {
preferences = object; preferences = object;
console.info("Succeeded in getting preferences."); console.info("Succeeded in getting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -191,11 +199,14 @@ FA模型示例: ...@@ -191,11 +199,14 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let preferences = null; let preferences: data_preferences.Preferences = null;
try { try {
data_preferences.getPreferences(context, { name: 'mystore' }, function (err, val) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.getPreferences(context, options, (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -213,13 +224,16 @@ Stage模型示例: ...@@ -213,13 +224,16 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
let preferences = null; let preferences: data_preferences.Preferences = null;
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, val) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -271,15 +285,17 @@ FA模型示例: ...@@ -271,15 +285,17 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let preferences = null; let preferences: data_preferences.Preferences = null;
try { try {
let promise = data_preferences.getPreferences(context, { name: 'mystore' }); let options: data_preferences.Options = { name: 'myStore' };
promise.then((object) => { let promise = data_preferences.getPreferences(context, options);
promise.then((object: data_preferences.Preferences) => {
preferences = object; preferences = object;
console.info("Succeeded in getting preferences."); console.info("Succeeded in getting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -291,17 +307,20 @@ Stage模型示例: ...@@ -291,17 +307,20 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
let preferences = null; let preferences: data_preferences.Preferences = null;
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
let promise = data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
promise.then((object) => { let promise = data_preferences.getPreferences(this.context, options);
promise.then((object: data_preferences.Preferences) => {
preferences = object; preferences = object;
console.info("Succeeded in getting preferences."); console.info("Succeeded in getting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -348,11 +367,14 @@ FA模型示例: ...@@ -348,11 +367,14 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let preferences = null; let preferences: data_preferences.Preferences = null;
try { try {
preferences = data_preferences.getPreferencesSync(context, { name: 'mystore' }); let options: data_preferences.Options = { name: 'myStore' };
preferences = data_preferences.getPreferencesSync(context, options);
} catch(err) { } catch(err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -362,12 +384,16 @@ Stage模型示例: ...@@ -362,12 +384,16 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
let preferences = null; import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
let preferences: data_preferences.Preferences = null;
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
preferences = data_preferences.getPreferencesSync(this.context, { name: 'mystore', dataGroupId:'myId' }); let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
preferences = data_preferences.getPreferencesSync(this.context, options);
} catch(err) { } catch(err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -408,10 +434,12 @@ FA模型示例: ...@@ -408,10 +434,12 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.deletePreferences(context, 'mystore', function (err) { data_preferences.deletePreferences(context, 'myStore', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -427,11 +455,13 @@ Stage模型示例: ...@@ -427,11 +455,13 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.deletePreferences(this.context, 'mystore', function (err) { data_preferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -483,13 +513,15 @@ FA模型示例: ...@@ -483,13 +513,15 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
let promise = data_preferences.deletePreferences(context, 'mystore'); let promise = data_preferences.deletePreferences(context, 'myStore');
promise.then(() => { promise.then(() => {
console.info("Succeeded in deleting preferences."); console.info("Succeeded in deleting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -501,14 +533,16 @@ Stage模型示例: ...@@ -501,14 +533,16 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try{ try{
let promise = data_preferences.deletePreferences(this.context, 'mystore'); let promise = data_preferences.deletePreferences(this.context, 'myStore');
promise.then(() => { promise.then(() => {
console.info("Succeeded in deleting preferences."); console.info("Succeeded in deleting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -553,10 +587,13 @@ FA模型示例: ...@@ -553,10 +587,13 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.deletePreferences(context, { name: 'mystore' }, function (err) { let options: data_preferences.Options = { name: 'myStore' };
data_preferences.deletePreferences(context, options, (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -572,11 +609,14 @@ Stage模型示例: ...@@ -572,11 +609,14 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.deletePreferences(this.context, options, (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -631,13 +671,16 @@ FA模型示例: ...@@ -631,13 +671,16 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
let promise = data_preferences.deletePreferences(context, { name: 'mystore' }); let options: data_preferences.Options = { name: 'myStore' };
let promise = data_preferences.deletePreferences(context, options);
promise.then(() => { promise.then(() => {
console.info("Succeeded in deleting preferences."); console.info("Succeeded in deleting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -649,14 +692,17 @@ Stage模型示例: ...@@ -649,14 +692,17 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try{ try{
let promise = data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
let promise = data_preferences.deletePreferences(this.context, options);
promise.then(() => { promise.then(() => {
console.info("Succeeded in deleting preferences."); console.info("Succeeded in deleting preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -694,9 +740,11 @@ FA模型示例: ...@@ -694,9 +740,11 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.removePreferencesFromCache(context, 'mystore', function (err) { data_preferences.removePreferencesFromCache(context, 'myStore', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -712,11 +760,13 @@ Stage模型示例: ...@@ -712,11 +760,13 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { data_preferences.removePreferencesFromCache(this.context, 'myStore', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -762,12 +812,14 @@ FA模型示例: ...@@ -762,12 +812,14 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
let promise = data_preferences.removePreferencesFromCache(context, 'mystore'); let promise = data_preferences.removePreferencesFromCache(context, 'myStore');
promise.then(() => { promise.then(() => {
console.info("Succeeded in removing preferences."); console.info("Succeeded in removing preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -779,14 +831,16 @@ Stage模型示例: ...@@ -779,14 +831,16 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore'); let promise = data_preferences.removePreferencesFromCache(this.context, 'myStore');
promise.then(() => { promise.then(() => {
console.info("Succeeded in removing preferences."); console.info("Succeeded in removing preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -824,7 +878,7 @@ FA模型示例: ...@@ -824,7 +878,7 @@ FA模型示例:
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.removePreferencesFromCacheSync(context, 'mystore'); data_preferences.removePreferencesFromCacheSync(context, 'myStore');
} catch(err) { } catch(err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -836,9 +890,9 @@ Stage模型示例: ...@@ -836,9 +890,9 @@ Stage模型示例:
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.removePreferencesFromCacheSync(this.context, 'mystore'); data_preferences.removePreferencesFromCacheSync(this.context, 'myStore');
} catch(err) { } catch(err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -882,9 +936,11 @@ FA模型示例: ...@@ -882,9 +936,11 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.removePreferencesFromCache(context, { name: 'mystore' }, function (err) { let options: data_preferences.Options = { name: 'myStore' };
data_preferences.removePreferencesFromCache(context, options, (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -900,11 +956,14 @@ Stage模型示例: ...@@ -900,11 +956,14 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -959,12 +1018,14 @@ FA模型示例: ...@@ -959,12 +1018,14 @@ FA模型示例:
```js ```js
// 获取context // 获取context
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base'
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
let promise = data_preferences.removePreferencesFromCache(context, { name: 'mystore' }); let options: data_preferences.Options = { name: 'myStore' };
let promise = data_preferences.removePreferencesFromCache(context, options);
promise.then(() => { promise.then(() => {
console.info("Succeeded in removing preferences."); console.info("Succeeded in removing preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -976,14 +1037,17 @@ Stage模型示例: ...@@ -976,14 +1037,17 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base'
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
let promise = data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }); let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
let promise = data_preferences.removePreferencesFromCache(this.context, options);
promise.then(() => { promise.then(() => {
console.info("Succeeded in removing preferences."); console.info("Succeeded in removing preferences.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1030,7 +1094,8 @@ FA模型示例: ...@@ -1030,7 +1094,8 @@ FA模型示例:
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext(); let context = featureAbility.getContext();
try { try {
data_preferences.removePreferencesFromCacheSync(context, { name: 'mystore' }); let options: data_preferences.Options = { name: 'myStore' };
data_preferences.removePreferencesFromCacheSync(context, options);
} catch(err) { } catch(err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -1040,11 +1105,13 @@ Stage模型示例: ...@@ -1040,11 +1105,13 @@ Stage模型示例:
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
try { try {
data_preferences.removePreferencesFromCacheSync(this.context, { name: 'mystore', dataGroupId:'myId' }); let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.removePreferencesFromCacheSync(this.context, options);
} catch(err) { } catch(err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
} }
...@@ -1090,7 +1157,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): ...@@ -1090,7 +1157,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;):
```js ```js
try { try {
preferences.get('startup', 'default', function (err, val) { preferences.get('startup', 'default', (err: BusinessError, val: data_preferences.ValueType) => {
if (err) { if (err) {
console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1129,9 +1196,9 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt; ...@@ -1129,9 +1196,9 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
```js ```js
try { try {
let promise = preferences.get('startup', 'default'); let promise = preferences.get('startup', 'default');
promise.then((data) => { promise.then((data: data_preferences.ValueType) => {
console.info("Succeeded in getting value of 'startup'. Data: " + data); console.info("Succeeded in getting value of 'startup'. Data: " + data);
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1164,7 +1231,7 @@ getSync(key: string, defValue: ValueType): ValueType ...@@ -1164,7 +1231,7 @@ getSync(key: string, defValue: ValueType): ValueType
```js ```js
try { try {
let value = preferences.getSync('startup', 'default'); let value: data_preferences.ValueType = preferences.getSync('startup', 'default');
console.info("Succeeded in getting value of 'startup'. Data: " + value); console.info("Succeeded in getting value of 'startup'. Data: " + value);
} catch(err) { } catch(err) {
console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
...@@ -1189,7 +1256,7 @@ getAll(callback: AsyncCallback&lt;Object&gt;): void; ...@@ -1189,7 +1256,7 @@ getAll(callback: AsyncCallback&lt;Object&gt;): void;
```js ```js
try { try {
preferences.getAll(function (err, value) { preferences.getAll((err: BusinessError, value: data_preferences.ValueType) => {
if (err) { if (err) {
console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1223,11 +1290,11 @@ getAll(): Promise&lt;Object&gt; ...@@ -1223,11 +1290,11 @@ getAll(): Promise&lt;Object&gt;
```js ```js
try { try {
let promise = preferences.getAll(); let promise = preferences.getAll();
promise.then((value) => { promise.then((value: data_preferences.ValueType) => {
let allKeys = Object.keys(value); let allKeys = Object.keys(value);
console.info('getAll keys = ' + allKeys); console.info('getAll keys = ' + allKeys);
console.info("getAll object = " + JSON.stringify(value)); console.info("getAll object = " + JSON.stringify(value));
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
}) })
} catch (err) { } catch (err) {
...@@ -1282,7 +1349,7 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void ...@@ -1282,7 +1349,7 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void
```js ```js
try { try {
preferences.put('startup', 'auto', function (err) { preferences.put('startup', 'auto', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1323,7 +1390,7 @@ try { ...@@ -1323,7 +1390,7 @@ try {
let promise = preferences.put('startup', 'auto'); let promise = preferences.put('startup', 'auto');
promise.then(() => { promise.then(() => {
console.info("Succeeded in putting value of 'startup'."); console.info("Succeeded in putting value of 'startup'.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message); console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1377,7 +1444,7 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): void ...@@ -1377,7 +1444,7 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): void
```js ```js
try { try {
preferences.has('startup', function (err, val) { preferences.has('startup', (err: BusinessError, val: boolean) => {
if (err) { if (err) {
console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1419,13 +1486,13 @@ has(key: string): Promise&lt;boolean&gt; ...@@ -1419,13 +1486,13 @@ has(key: string): Promise&lt;boolean&gt;
```js ```js
try { try {
let promise = preferences.has('startup'); let promise = preferences.has('startup');
promise.then((val) => { promise.then((val: boolean) => {
if (val) { if (val) {
console.info("The key 'startup' is contained."); console.info("The key 'startup' is contained.");
} else { } else {
console.info("The key 'startup' dose not contain."); console.info("The key 'startup' dose not contain.");
} }
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1458,7 +1525,7 @@ hasSync(key: string): boolean ...@@ -1458,7 +1525,7 @@ hasSync(key: string): boolean
```js ```js
try { try {
let isExist = preferences.hasSync('startup'); let isExist: boolean = preferences.hasSync('startup');
if (isExist) { if (isExist) {
console.info("The key 'startup' is contained."); console.info("The key 'startup' is contained.");
} else { } else {
...@@ -1489,7 +1556,7 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -1489,7 +1556,7 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void
```js ```js
try { try {
preferences.delete('startup', function (err) { preferences.delete('startup', (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1529,7 +1596,7 @@ try { ...@@ -1529,7 +1596,7 @@ try {
let promise = preferences.delete('startup'); let promise = preferences.delete('startup');
promise.then(() => { promise.then(() => {
console.info("Succeeded in deleting the key 'startup'."); console.info("Succeeded in deleting the key 'startup'.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1581,7 +1648,7 @@ flush(callback: AsyncCallback&lt;void&gt;): void ...@@ -1581,7 +1648,7 @@ flush(callback: AsyncCallback&lt;void&gt;): void
```js ```js
try { try {
preferences.flush(function (err) { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to flush. code =" + err.code + ", message =" + err.message); console.error("Failed to flush. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1615,7 +1682,7 @@ try { ...@@ -1615,7 +1682,7 @@ try {
let promise = preferences.flush(); let promise = preferences.flush();
promise.then(() => { promise.then(() => {
console.info("Succeeded in flushing."); console.info("Succeeded in flushing.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to flush. code =" + err.code + ", message =" + err.message); console.error("Failed to flush. code =" + err.code + ", message =" + err.message);
}) })
} catch (err) { } catch (err) {
...@@ -1642,7 +1709,7 @@ clear(callback: AsyncCallback&lt;void&gt;): void ...@@ -1642,7 +1709,7 @@ clear(callback: AsyncCallback&lt;void&gt;): void
```js ```js
try { try {
preferences.clear(function (err) { preferences.clear((err: BusinessError) =>{
if (err) { if (err) {
console.error("Failed to clear. code =" + err.code + ", message =" + err.message); console.error("Failed to clear. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1676,7 +1743,7 @@ try { ...@@ -1676,7 +1743,7 @@ try {
let promise = preferences.clear(); let promise = preferences.clear();
promise.then(() => { promise.then(() => {
console.info("Succeeded in clearing."); console.info("Succeeded in clearing.");
}).catch((err) => { }).catch((err: BusinessError) => {
console.error("Failed to clear. code =" + err.code + ", message =" + err.message); console.error("Failed to clear. code =" + err.code + ", message =" + err.message);
}) })
} catch(err) { } catch(err) {
...@@ -1722,24 +1789,26 @@ on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void ...@@ -1722,24 +1789,26 @@ on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void
**示例:** **示例:**
```js ```js
interface observer {
key: string
}
try { try {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences."); console.error("Failed to get preferences.");
return; return;
} }
let observer = function (key) { preferences.on('change', (key: observer) => {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
} });
preferences.on('change', observer); preferences.put('startup', 'manual', (err: BusinessError) => {
preferences.put('startup', 'manual', function (err) {
if (err) { if (err) {
console.error("Failed to put the value of 'startup'. Cause: " + err); console.error("Failed to put the value of 'startup'. Cause: " + err);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush(function (err) { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to flush. Cause: " + err); console.error("Failed to flush. Cause: " + err);
return; return;
...@@ -1781,24 +1850,26 @@ on(type: 'multiProcessChange', callback: Callback&lt;{ key : string }&gt;): void ...@@ -1781,24 +1850,26 @@ on(type: 'multiProcessChange', callback: Callback&lt;{ key : string }&gt;): void
**示例1:** **示例1:**
```js ```js
interface observer {
key: string
}
try { try {
data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.getPreferences(this.context, options, (err: BusinessError, preferences: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences."); console.error("Failed to get preferences.");
return; return;
} }
let observer = function (key) { preferences.on('multiProcessChange', (key: observer) => {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
} });
preferences.on('multiProcessChange', observer); preferences.put('startup', 'manual', (err: BusinessError) => {
preferences.put('startup', 'manual', function (err) {
if (err) { if (err) {
console.error("Failed to put the value of 'startup'. Cause: " + err); console.error("Failed to put the value of 'startup'. Cause: " + err);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush((err: BusinessError) => {
preferences.flush(function (err) {
if (err) { if (err) {
console.error("Failed to flush. Cause: " + err); console.error("Failed to flush. Cause: " + err);
return; return;
...@@ -1815,17 +1886,21 @@ try { ...@@ -1815,17 +1886,21 @@ try {
**示例2:** **示例2:**
```js ```js
interface observer {
key: string
}
try { try {
data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { let options: data_preferences.Options = { name: 'myStore' };
data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences."); console.error("Failed to get preferences.");
return; return;
} }
preferences = val; preferences = val;
let observer = function (key) { preferences.on('multiProcessChange', (key: observer) => {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
try { try {
data_preferences.removePreferencesFromCache(this.context, { name: 'mystore' }, function (err) { data_preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1838,7 +1913,7 @@ try { ...@@ -1838,7 +1913,7 @@ try {
} }
try { try {
data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return; return;
...@@ -1849,16 +1924,15 @@ try { ...@@ -1849,16 +1924,15 @@ try {
} catch (err) { } catch (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
} }
} });
preferences.on('multiProcessChange', observer); preferences.put('startup', 'manual', (err: BusinessError) => {
preferences.put('startup', 'manual', function (err) {
if (err) { if (err) {
console.error("Failed to put the value of 'startup'. Cause: " + err); console.error("Failed to put the value of 'startup'. Cause: " + err);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush(function (err) { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to flush. Cause: " + err); console.error("Failed to flush. Cause: " + err);
return; return;
...@@ -1890,31 +1964,35 @@ off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void ...@@ -1890,31 +1964,35 @@ off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void
**示例:** **示例:**
```js ```js
interface observer {
key: string
}
try { try {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences."); console.error("Failed to get preferences.");
return; return;
} }
let observer = function (key) { preferences.on('change', (key: observer) => {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
} });
preferences.on('change', observer); preferences.put('startup', 'auto', (err: BusinessError) => {
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.error("Failed to put the value of 'startup'. Cause: " + err); console.error("Failed to put the value of 'startup'. Cause: " + err);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush(function (err) { preferences.flush((err: BusinessError) =>{
if (err) { if (err) {
console.error("Failed to flush. Cause: " + err); console.error("Failed to flush. Cause: " + err);
return; return;
} }
console.info("Succeeded in flushing."); console.info("Succeeded in flushing.");
}) })
preferences.off('change', observer); preferences.off('change', (key: observer) => {
console.info("The key " + key + " changed.");
});
}) })
}) })
} catch (err) { } catch (err) {
...@@ -1940,31 +2018,36 @@ off(type: 'multiProcessChange', callback?: Callback&lt;{ key : string }&gt;): vo ...@@ -1940,31 +2018,36 @@ off(type: 'multiProcessChange', callback?: Callback&lt;{ key : string }&gt;): vo
**示例:** **示例:**
```js ```js
interface observer {
key: string
}
try { try {
data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' };
data_preferences.getPreferences(this.context, options, (err: BusinessError, preferences: data_preferences.Preferences) => {
if (err) { if (err) {
console.error("Failed to get preferences."); console.error("Failed to get preferences.");
return; return;
} }
let observer = function (key) { preferences.on('multiProcessChange', (key: observer) => {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
} });
preferences.on('multiProcessChange', observer); preferences.put('startup', 'auto', (err: BusinessError) => {
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.error("Failed to put the value of 'startup'. Cause: " + err); console.error("Failed to put the value of 'startup'. Cause: " + err);
return; return;
} }
console.info("Succeeded in putting the value of 'startup'."); console.info("Succeeded in putting the value of 'startup'.");
preferences.flush(function (err) { preferences.flush((err: BusinessError) => {
if (err) { if (err) {
console.error("Failed to flush. Cause: " + err); console.error("Failed to flush. Cause: " + err);
return; return;
} }
console.info("Succeeded in flushing."); console.info("Succeeded in flushing.");
}) })
preferences.off('multiProcessChange', observer); preferences.off('multiProcessChange', (key: observer) => {
console.info("The key " + key + " changed.");
});
}) })
}) })
} catch (err) { } catch (err) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册