未验证 提交 ae7625f6 编写于 作者: O openharmony_ci 提交者: Gitee

!8305 [翻译完成】#I5KJFC

Merge pull request !8305 from Annie_wang/PR7696
...@@ -17,7 +17,7 @@ Call **createDistributedObject()** to create a distributed data object instance. ...@@ -17,7 +17,7 @@ Call **createDistributedObject()** to create a distributed data object instance.
**Table 1** API for creating a distributed data object instance **Table 1** API for creating a distributed data object instance
| Package| API| Description| | Package| API| Description|
| -------- | -------- | -------- | | -------- | -------- | -------- |
| ohos.data.distributedDataObject| createDistributedObject(source: object): DistributedObject | Creates a distributed data object instance for data operations.<br>- **source**: attributes of the **distributedObject** set.<br>- **DistributedObject**: returns the distributed object created.| | ohos.data.distributedDataObject| createDistributedObject(source: object): DistributedObject | Creates a distributed data object instance for data operations.<br>- **source**: attributes of the **distributedObject** set.<br>- **DistributedObject**: returns the distributed object created.|
### Generating a Session ID ### Generating a Session ID
...@@ -35,16 +35,16 @@ Call **setSessionId()** to set a session ID for a distributed data object. The s ...@@ -35,16 +35,16 @@ Call **setSessionId()** to set a session ID for a distributed data object. The s
**Table 3** API for setting a session ID **Table 3** API for setting a session ID
| Class| API| Description| | 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 ### Observing Data Changes
Call **on()** to subscribe to data changes of a distributed data object. When the data changes, a callback will be invoked to return the data changes. You can use **off()** to unsubscribe from the data changes. Call **on()** to subscribe to data changes of a distributed data object. When the data changes, a callback will be invoked to return the data changes. You can use **off()** to unsubscribe from the data changes.
**Table 4** APIs for observing data changes of a distributed data object **Table 4** APIs for observing data changes of a distributed data object
| Class| API| Description| | Class| API| Description|
| -------- | -------- | -------- | | -------- | -------- | -------- |
| DistributedDataObject| on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void | Subscribes to data changes.| | DistributedDataObject| on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void | Subscribes to data changes.|
| DistributedDataObject| off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void | Unsubscribes from data changes. **Callback**: specifies callback used to return changes of the distributed data object. If this parameter is not specified, all callbacks related to data changes will be unregistered.| | DistributedDataObject| off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void | Unsubscribes from data changes. **Callback**: specifies callback used to return changes of the distributed data object. If this parameter is not specified, all callbacks related to data changes will be unregistered.|
### Observing Online or Offline Status ### Observing Online or Offline Status
...@@ -85,7 +85,11 @@ The following example shows how to implement a distributed data object synchroni ...@@ -85,7 +85,11 @@ The following example shows how to implement a distributed data object synchroni
```js ```js
import distributedObject from '@ohos.data.distributedDataObject'; import distributedObject from '@ohos.data.distributedDataObject';
``` ```
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": { "module": {
...@@ -97,10 +101,11 @@ The following example shows how to implement a distributed data object synchroni ...@@ -97,10 +101,11 @@ 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: 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'; import featureAbility from '@ohos.ability.featureAbility';
function grantPermission() { function grantPermission() {
console.info('grantPermission'); console.info('grantPermission');
let context = featureAbility.getContext(); let context = featureAbility.getContext();
...@@ -108,11 +113,13 @@ The following example shows how to implement a distributed data object synchroni ...@@ -108,11 +113,13 @@ The following example shows how to implement a distributed data object synchroni
console.info(`result.requestCode=${result.requestCode}`) console.info(`result.requestCode=${result.requestCode}`)
}) })
console.info('end grantPermission'); console.info('end grantPermission');
} }
grantPermission(); grantPermission();
``` ```
3. Obtain a distributed data object instance. 3. Obtain a distributed data object instance.
The sample code is as follows: The sample code is as follows:
...@@ -130,7 +137,7 @@ The following example shows how to implement a distributed data object synchroni ...@@ -130,7 +137,7 @@ The following example shows how to implement a distributed data object synchroni
```js ```js
// Local object // Local object
var local_object = distributedObject.createDistributedObject({name:"jack", age:18, isVis:true, var local_object = distributedObject.createDistributedObject({name:"jack", age:18, isVis:true,
parent:{mother:"jack mom",father:"jack Dad"},list:[{mother:"jack mom"}, {father:"jack Dad"}]}); parent:{mother:"jack mom", father:"jack Dad"}, list:[{mother:"jack mom"}, {father:"jack Dad"}]});
local_object.setSessionId(sessionId); local_object.setSessionId(sessionId);
// Remote object // Remote object
...@@ -140,8 +147,10 @@ The following example shows how to implement a distributed data object synchroni ...@@ -140,8 +147,10 @@ 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. // 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: The sample code is as follows:
```js ```js
...@@ -152,26 +161,32 @@ The following example shows how to implement a distributed data object synchroni ...@@ -152,26 +161,32 @@ The following example shows how to implement a distributed data object synchroni
changeData.forEach(element => { changeData.forEach(element => {
console.info("changed !" + element + " " + local_object[element]); console.info("changed !" + element + " " + local_object[element]);
}); });
} }
} }
// To refresh the page in changeCallback, correctly bind (this) to the changeCallback. // To refresh the page in changeCallback, correctly bind (this) to the changeCallback.
local_object.on("change", this.changeCallback.bind(this)); local_object.on("change", this.changeCallback.bind(this));
``` ```
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).
The sample code is as follows: The sample code is as follows:
```js ```js
local_object.name = "jack"; local_object.name = "jack";
local_object.age = 19; local_object.age = 19;
local_object.isVis = false; local_object.isVis = false;
local_object.parent = {mother:"jack mom",father:"jack Dad"}; local_object.parent = {mother:"jack mom", father:"jack Dad"};
local_object.list = [{mother:"jack mom"}, {father:"jack Dad"}]; local_object.list = [{mother:"jack mom"}, {father:"jack Dad"}];
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br> > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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 ```js
// Supported modification. // Supported modification.
local_object.parent = {mother:"mom", father:"dad"}; local_object.parent = {mother:"mom", father:"dad"};
...@@ -179,13 +194,18 @@ The following example shows how to implement a distributed data object synchroni ...@@ -179,13 +194,18 @@ The following example shows how to implement a distributed data object synchroni
local_object.parent.mother = "mom"; 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: The sample code is as follows:
```js ```js
console.info("name " + local_object["name"]); 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: The sample code is as follows:
```js ```js
...@@ -194,6 +214,7 @@ The following example shows how to implement a distributed data object synchroni ...@@ -194,6 +214,7 @@ The following example shows how to implement a distributed data object synchroni
// Unregister all data change callbacks. // Unregister all data change callbacks.
local_object.off("change"); 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. 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: The sample code is as follows:
```js ```js
...@@ -207,58 +228,62 @@ The following example shows how to implement a distributed data object synchroni ...@@ -207,58 +228,62 @@ The following example shows how to implement a distributed data object synchroni
10. Save a distributed data object and revoke the data saving operation. 10. Save a distributed data object and revoke the data saving operation.
- Callback - Callback
```js ```
// Save a distributed data object. ​```js
local_object.save("local", (result, data)=>{ // Save a distributed data object.
console.log("save callback"); local_object.save("local", (result, data) => {
console.info("save sessionId " + data.sessionId); console.log("save callback");
console.info("save version " + data.version); console.info("save sessionId " + data.sessionId);
console.info("save deviceId " + data.deviceId); console.info("save version " + data.version);
}); console.info("save deviceId " + data.deviceId);
// Revoke the data saving operation. });
local_object.revokeSave((result, data) =>{ // Revoke the data saving operation.
console.log("revokeSave callback"); local_object.revokeSave((result, data) => {
console.info("revokeSave sessionId " + data.sessionId); console.log("revokeSave callback");
}); console.info("revokeSave sessionId " + data.sessionId);
``` });
- Promise ​```
```js ```
// Save a distributed data object.
g_object.save("local").then((result)=>{ - Promise
console.info("save sessionId " + result.sessionId);
console.info("save version " + result.version); ```
console.info("save deviceId " + result.deviceId); ​```js
}, (result)=>{ // Save a distributed data object.
console.info("save local failed."); g_object.save("local").then((result) => {
}); console.info("save sessionId " + result.sessionId);
// Revoke the data saving operation. console.info("save version " + result.version);
g_object.revokeSave().then((result)=>{ console.info("save deviceId " + result.deviceId);
console.info("revokeSave success."); }, (result)=>{
}, (result)=>{ console.info("save local failed.");
console.info("revokeSave failed."); });
}); // Revoke the data saving operation.
``` g_object.revokeSave().then((result) => {
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. console.info("revokeSave success.");
}, (result)=>{
console.info("revokeSave failed.");
});
​```
```
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: The sample code is as follows:
```js ```js
// Unregister the specified status change callback. // Unregister the specified status change callback.
local_object.off("status", this.statusCallback); local_object.off("status", this.statusCallback);
// Unregister all status change callbacks. // Unregister all status change callbacks.
local_object.off("status"); 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. 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: The sample code is as follows:
```js ```js
local_object.setSessionId(""); 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.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册