@@ -35,7 +35,7 @@ Call **setSessionId()** to set a session ID for a distributed data object. The s
**Table 3** API for setting a session ID
| Class| API| Description|
| -------- | -------- | -------- |
| DistributedDataObject | setSessionId(sessionId?: string): boolean | Sets a session ID for distributed data objects.<br>**sessionId**: session ID of a distributed object in a trusted network. To remove a distributed data object from the network, set this parameter to "" or leave it empty.|
| DistributedDataObject | setSessionId(sessionId?: string): boolean | Sets a session ID for distributed data objects.<br>**sessionId**: session ID of a distributed object in a trusted network. To remove a distributed data object from the network, set this parameter to "" or leave it empty.|
### Observing Data Changes
...
...
@@ -85,7 +85,11 @@ The following example shows how to implement a distributed data object synchroni
2. Request the permission. <br>Add the required permission in the **config.json** file. The sample code is as follows:
2. Request the permission.
Add the required permission in the **config.json** file. The sample code is as follows:
```
{
"module": {
...
...
@@ -98,7 +102,8 @@ The following example shows how to implement a distributed data object synchroni
}
```
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() {
...
...
@@ -113,6 +118,8 @@ The following example shows how to implement a distributed data object synchroni
grantPermission();
```
3. Obtain a distributed data object instance.
The sample code is as follows:
...
...
@@ -130,7 +137,7 @@ The following example shows how to implement a distributed data object synchroni
@@ -140,7 +147,9 @@ 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.
```
5. Observe the data changes of the distributed data object. <br>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.
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:
...
...
@@ -159,19 +168,25 @@ The following example shows how to implement a distributed data object synchroni
6. Modify object attributes. <br>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).
> For the distributed data object of the complex type, only the root attribute can be modified. The subordinate attributes cannot be modified. Example:
> For the distributed data object of the complex type, only the root attribute can be modified. The subordinate attributes cannot be modified.
Example:
```js
// Supported modification.
local_object.parent={mother:"mom",father:"dad"};
...
...
@@ -179,13 +194,18 @@ The following example shows how to implement a distributed data object synchroni
local_object.parent.mother="mom";
```
7. Access the distributed data object. <br>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"]);
```
8. Unsubscribe from data changes. <br>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.
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
...
...
@@ -194,6 +214,7 @@ The following example shows how to implement a distributed data object synchroni
// Unregister all data change callbacks.
local_object.off("change");
```
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
...
...
@@ -208,24 +229,29 @@ The following example shows how to implement a distributed data object synchroni
@@ -233,13 +259,19 @@ The following example shows how to implement a distributed data object synchroni
console.info("save local failed.");
});
// Revoke the data saving operation.
g_object.revokeSave().then((result)=>{
g_object.revokeSave().then((result) => {
console.info("revokeSave success.");
}, (result)=>{
console.info("revokeSave failed.");
});
```
```
11. Unsubscribe from the status changes of the distributed data object. <br>You can specify the callback to unregister. If you do not specify the callback, this API unregisters all callbacks of this distributed data object.
11. 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 unregisters all callbacks of this distributed data object.
The sample code is as follows:
```js
...
...
@@ -248,17 +280,10 @@ The following example shows how to implement a distributed data object synchroni
// Unregister all status change callbacks.
local_object.off("status");
```
12. 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:
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.