From d507b37e1ac4aa337a8fc546ae0b55f7f6e15ce5 Mon Sep 17 00:00:00 2001 From: Annie_wang Date: Sat, 25 Jun 2022 14:22:13 +0800 Subject: [PATCH] update docs Signed-off-by: Annie_wang --- .../database-distributedobject-guidelines.md | 69 ++++++-- .../apis/js-apis-data-distributedobject.md | 156 +++++++++--------- 2 files changed, 129 insertions(+), 96 deletions(-) diff --git a/en/application-dev/database/database-distributedobject-guidelines.md b/en/application-dev/database/database-distributedobject-guidelines.md index 303b306528..78d2cc89ff 100644 --- a/en/application-dev/database/database-distributedobject-guidelines.md +++ b/en/application-dev/database/database-distributedobject-guidelines.md @@ -62,11 +62,38 @@ Call **on()** to subscribe to status changes of a distributed data object. The s The following example shows how to implement a distributed data object synchronization. 1. Import the @ohos.data.distributedDataObject module to the development environment. - ```js - import distributedObject from '@ohos.data.distributedDataObject' - ``` + ```js + import distributedObject from '@ohos.data.distributedDataObject'; + ``` +2. Request the permission.
Add the required permission in the **config.json** file. The sample code is as follows: + ``` + { + "module": { + "reqPermissions": [ + { + "name": "ohos.permission.DISTRIBUTED_DATASYNC" + } + ] + } + } + ``` + This permission must also be authorized by the user through a dialog box when the application is started for the first time. The sample code is as follows: + ```js + import featureAbility from '@ohos.ability.featureAbility'; + + function grantPermission() { + console.info('grantPermission'); + let context = featureAbility.getContext(); + context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) { + console.info(`result.requestCode=${result.requestCode}`) + + }) + console.info('end grantPermission'); + } + grantPermission(); + ``` -2. Obtain a distributed data object instance. +3. Obtain a distributed data object instance. The sample code is as follows: ```js @@ -76,7 +103,7 @@ The following example shows how to implement a distributed data object synchroni ``` -3. Add the synchronization network. The data objects in the synchronization network include the local and remote objects. +4. Add the synchronization network. The data objects in the synchronization network include the local and remote objects. The sample code is as follows: @@ -93,7 +120,7 @@ The following example shows how to implement a distributed data object synchroni // After learning that the device goes online, the remote object synchronizes data. That is, name changes to jack and age to 18. ``` -4. Observe the data changes of the distributed data object. You can subscribe to data changes of the remote object. When the data in the remote object changes, a callback will be called to return the data changes. +5. Observe the data changes of the distributed data object.
You can subscribe to data changes of the peer object. When the data in the peer object changes, a callback will be called to return the data changes. The sample code is as follows: @@ -107,12 +134,12 @@ The following example shows how to implement a distributed data object synchroni }); } } - + // To refresh the page in changeCallback, correctly bind (this) to the changeCallback. local_object.on("change", this.changeCallback.bind(this)); ``` -5. Modify object attributes. The object attributes support basic data types (such as number, Boolean, and string) and complex data types (array and nested basic types). +6. Modify object attributes. The object attributes support basic data types (such as number, Boolean, and string) and complex data types (array and nested basic types). The sample code is as follows: ```js @@ -132,22 +159,22 @@ The following example shows how to implement a distributed data object synchroni local_object.parent.mother = "mom"; ``` -6. Access the distributed data object. Obtain the distributed data object attribute, which is the latest data on the network. +7. Access the distributed data object. Obtain the distributed data object attribute, which is the latest data on the network. The sample code is as follows: ```js console.info("name " + local_object["name"]); ``` -7. Unsubscribe from data changes. You can specify the callback to unsubscribe from. If you do not specify the callback, all data change callbacks of the distributed data object will be unsubscribed from. +8. Unsubscribe from data changes.
You can specify the callback to unregister. If you do not specify the callback, all data change callbacks of the distributed data object will be unregistered. The sample code is as follows: ```js - // Unsubscribe from the specified data change callback. + // Unregister the specified data change callback. local_object.off("change", changeCallback); - // Unsubscribe from all data change callbacks. + // Unregister all data change callbacks. local_object.off("change"); ``` -8. Subscribe to the status (online/offline) changes of the distributed data object. A callback will be invoked to report the status change when the target distributed data object goes online or offline. +9. Subscribe to the status (online/offline) changes of the distributed data object. A callback will be invoked to report the status change when the target distributed data object goes online or offline. The sample code is as follows: ```js function statusCallback(sessionId, networkId, status) { @@ -156,19 +183,27 @@ The following example shows how to implement a distributed data object synchroni local_object.on("status", this.statusCallback); ``` -9. Unsubscribe from the status changes of the distributed data object. You can specify the callback to unsubscribe from. If you do not specify the callback, this API unsubscribes from all callbacks of this distributed data object. +10. Unsubscribe from the status changes of the distributed data object.
You can specify the callback to unregister. If you do not specify the callback, this API unregister all callbacks of this distributed data object. The sample code is as follows: ```js - // Unsubscribe from the specified status change callback. + // Unregister the specified status change callback. local_object.off("status", statusCallback); - // Unsubscribe from all status change callbacks. + // Unregister all status change callbacks. local_object.off("status"); ``` -10. Remove a distributed data object from the synchronization network. Data changes on the local object will not be synchronized to the removed distributed data object. +11. Remove a distributed data object from the synchronization network. Data changes on the local object will not be synchronized to the removed distributed data object. The sample code is as follows: ```js local_object.setSessionId(""); ``` +## Development Example + +The following example is provided for you to better understand the development of distributed data objects: + +- [Distributed Notepad](https://gitee.com/openharmony/distributeddatamgr_objectstore/tree/master/samples/distributedNotepad) + +When an event of the Notepad app occurs on a device, such as a note is added, the tile or content of a note is changed, or the event list is cleared, the change will be synchronized to other devices in the trusted network. + diff --git a/en/application-dev/reference/apis/js-apis-data-distributedobject.md b/en/application-dev/reference/apis/js-apis-data-distributedobject.md index 36761408ca..d121a8f840 100644 --- a/en/application-dev/reference/apis/js-apis-data-distributedobject.md +++ b/en/application-dev/reference/apis/js-apis-data-distributedobject.md @@ -1,6 +1,9 @@ # Distributed Data Object +Provides basic data object management, including creating, querying, deleting, modifying, and subscribing to data objects, and distributed data object collaboration for the same application among multiple devices. + > **NOTE**
+> > The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. @@ -20,9 +23,9 @@ Creates a distributed data object. **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | source | object | Yes| Attribute of the distributed data object to create.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| source | object | Yes| Attribute of the distributed data object to create.| **Return value** | Type| Description| @@ -31,14 +34,14 @@ Creates a distributed data object. **Example** ```js - import distributedObject from '@ohos.data.distributedDataObject' + import distributedObject from '@ohos.data.distributedDataObject'; // Create a distributed data object, which contains attributes of four types, namely, string, number, boolean, and object. var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); ``` -## distributedObject.genSessionId() +## distributedObject.genSessionId genSessionId(): string @@ -47,13 +50,13 @@ Creates a random session ID. **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Return value** - | Type| Description| - | -------- | -------- | - | string | Session ID created.| +| Type| Description| +| -------- | -------- | +| string | Session ID created.| **Example** ```js - import distributedObject from '@ohos.data.distributedDataObject' + import distributedObject from '@ohos.data.distributedDataObject'; var sessionId = distributedObject.genSessionId(); ``` @@ -68,24 +71,26 @@ setSessionId(sessionId?: string): boolean Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network. +**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC + **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | sessionId | string | No| ID of a distributed data object on a trusted network. To remove a distributed data object from the network, set this parameter to "" or leave it empty.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| sessionId | string | No| ID of a distributed data object on a trusted network. To remove a distributed data object from the network, set this parameter to "" or leave it empty.| **Return value** - | Type| Description| - | -------- | -------- | - | boolean | Returns **true** if the session ID is set successfully;
returns **false** otherwise. | +| Type| Description| +| -------- | -------- | +| boolean | Returns **true** if the session ID is set successfully;
returns **false** otherwise. | **Example** ```js - import distributedObject from '@ohos.data.distributedDataObject' + import distributedObject from '@ohos.data.distributedDataObject'; var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); // Add g_object to the distributed network. @@ -104,25 +109,25 @@ Subscribes to the changes of this distributed data object. **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data changes.| - | callback | Callback<{ sessionId: string, fields: Array<string> }> | Yes| Callback used to return the changes of the distributed data object.
**sessionId** indicates the session ID of the distributed data object.
**fields** indicates the changed attributes of the distributed data object.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data changes.| +| callback | Callback<{ sessionId: string, fields: Array<string> }> | Yes| Callback used to return the changes of the distributed data object.
**sessionId** indicates the session ID of the distributed data object.
**fields** indicates the changed attributes of the distributed data object.| **Example** - ```js - import distributedObject from '@ohos.data.distributedDataObject' - var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, - parent:{mother:"jack mom",father:"jack Dad"}}); - g_object.on("change", function (sessionId, changeData) { - console.info("change" + sessionId); - if (changeData != null && changeData != undefined) { - changeData.forEach(element => { - console.info("changed !" + element + " " + g_object[element]); - }); - } - }); - ``` +```js +import distributedObject from '@ohos.data.distributedDataObject'; +var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,parent:{mother:"jack mom",father:"jack Dad"}}); +globalThis.changeCallback = (sessionId, changeData) => { + console.info("change" + sessionId); + if (changeData != null && changeData != undefined) { + changeData.forEach(element => { + console.info("changed !" + element + " " + g_object[element]); + }); + } +} +g_object.on("change", globalThis.changeCallback); +``` ### off('change') @@ -133,27 +138,21 @@ Unsubscribes from the changes of this distributed data object. **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.| - | callback | Callback<{ sessionId: string, fields: Array<string> }> | No| Callback used to return the changes of the distributed data object. If this parameter is not specified, this API unsubscribes from all callbacks for data changes of this distributed data object.
**sessionId** indicates the session ID of the distributed data object.
**fields** indicates the changed attributes of the distributed data object.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.| +| callback | Callback<{ sessionId: string, fields: Array<string> }> | No| Callback to be unregistered. If this parameter is not set, all data change callbacks of the object will be unregistered.
**sessionId** indicates the session ID of the distributed data object.
**fields** indicates the changed attributes of the distributed data object.| **Example** - ```js - import distributedObject from '@ohos.data.distributedDataObject' - var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, - parent:{mother:"jack mom",father:"jack Dad"}}); - g_object.on("change", function (sessionId, changeData) { - console.info("change" + sessionId); - }); - // Unsubscribe from the specified data change callback for the distributed data object. - g_object.off("change", function (sessionId, changeData) { - console.info("change" + sessionId); - }); - // Unsubscribe from all data change callbacks for the distributed data object. - g_object.off("change"); - ``` +```js +import distributedObject from '@ohos.data.distributedDataObject'; +var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,parent:{mother:"jack mom",father:"jack Dad"}}); +// Unregister the specified data change callback. +g_object.off("change", globalThis.changeCallback); +// Unregister all data change callbacks. +g_object.off("change"); +``` ### on('status') @@ -164,20 +163,20 @@ Subscribes to the status change (online or offline) of this distributed data obj **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type to subscribe to. The value is **status**, which indicates the change in the status (online or offline) of the distributed data object.| - | callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | Yes| Callback used to return the status change.
**sessionId** indicates the session ID of the distributed data object.
**networkId** indicates the network ID of the device.
**status** indicates the status, which can be online or offline.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type to subscribe to. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| +| callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | Yes| Callback used to return the status change.
**sessionId** indicates the session ID of the distributed data object.
**networkId** indicates the network ID of the device.
**status** indicates the object status, which can be online or offline. | **Example** - ```js - import distributedObject from '@ohos.data.distributedDataObject' - var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, - parent:{mother:"jack mom",father:"jack Dad"}}); - g_object.on("status", function (sessionId, networkid, status) { - this.response += "status changed " + sessionId + " " + status + " " + networkId; - }); - ``` +```js +import distributedObject from '@ohos.data.distributedDataObject'; +globalThis.statusCallback = (sessionId, networkId, status) => { + globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; +} +var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,parent:{mother:"jack mom",father:"jack Dad"}}); +g_object.on("status", globalThis.statusCallback); +``` ### off('status') @@ -189,22 +188,21 @@ Unsubscribes from the status change (online or offline) of this distributed data **System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject **Parameters** - | Name| Type| Mandatory| Description| - | -------- | -------- | -------- | -------- | - | type | string | Yes| Event type to unsubscribe from. The value is **status**, which indicates the change in the status (online or offline) of the distributed data object.| - | callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback used to return the status change. If this parameter is not specified, this API unsubscribes from all callbacks of this distributed data object.
**sessionId** indicates the session ID of the distributed data object.
**deviceId** indicates the device ID of the distributed data object.
**status** indicates the status, which can be online or offline.| +| Name| Type| Mandatory| Description| +| -------- | -------- | -------- | -------- | +| type | string | Yes| Event type to unsubscribe from. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| +| callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback used to return the status change. If this parameter is not specified, this API unsubscribes from all callbacks of this distributed data object.
**sessionId** indicates the session ID of the distributed data object.
**deviceId** indicates the device ID of the distributed data object.
**status** indicates the status, which can be online or offline.| **Example** - ```js - import distributedObject from '@ohos.data.distributedDataObject' - g_object.on("status", function (sessionId, networkId, status) { - this.response += "status changed " + sessionId + " " + status + " " + networkId; - }); - // Unsubscribe from the specified status change callback for the distributed data object. - g_object.off("status", function (sessionId, networkId, status) { - this.response += "status changed " + sessionId + " " + status + " " + networkId; - }); - // Unsubscribe from all status change callbacks for the distributed data object. - g_object.off("status"); - ``` +```js +import distributedObject from '@ohos.data.distributedDataObject'; +var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false,parent:{mother:"jack mom",father:"jack Dad"}}); +globalThis.statusCallback = (sessionId, networkId, status) => { + globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; +} +// Unsubscribe from the specified status change callback for the distributed data object. +g_object.off("status",globalThis.statusCallback); +// Unsubscribe from all status change callbacks for the distributed data object. +g_object.off("status"); +``` -- GitLab