js-apis-data-distributedobject.md 8.6 KB
Newer Older
A
annie_wangli 已提交
1
# Distributed Data Object
A
annie_wangli 已提交
2 3 4 5 6 7 8 9

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> 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.


## Modules to Import

```js
A
Annie_wang 已提交
10
import distributedObject from '@ohos.data.distributedDataObject';
A
annie_wangli 已提交
11 12 13 14 15 16 17 18 19
```



## distributedDataObject.createDistributedObject

createDistributedObject(source: object): DistributedObject


A
annie_wangli 已提交
20
Creates a distributed data object.
A
annie_wangli 已提交
21

A
annie_wangli 已提交
22
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
23

A
annie_wangli 已提交
24
**Parameters**
A
annie_wangli 已提交
25 26
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
annie_wangli 已提交
27
  | source | object | Yes| Attribute of the distributed data object to create.|
A
Annie_wang 已提交
28

A
annie_wangli 已提交
29
**Return Value**
A
Annie_wang 已提交
30 31 32
| Type| Description|
| -------- | -------- |
| [DistributedObject](#distributedobject) | Distributed data object created.|
A
annie_wangli 已提交
33 34

**Example**
A
annie_wangli 已提交
35 36 37 38 39 40 41 42 43 44 45 46
  ```js
  import distributedObject from '@ohos.data.distributedDataObject'
  // Create a distributedObject instance. The attribute type of the object can be string, number, boolean, or Object.
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, 
                 parent:{mother:"jack mom",father:"jack Dad"}});
  ```


## distributedObject.genSessionId()

genSessionId(): string

A
annie_wangli 已提交
47
Creates a random session ID.
A
annie_wangli 已提交
48

A
annie_wangli 已提交
49
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
50

A
annie_wangli 已提交
51
**Return Value**
A
annie_wangli 已提交
52 53 54 55
  | Type| Description|
  | -------- | -------- |
  | string | Session ID created.|

A
annie_wangli 已提交
56
**Example**
A
annie_wangli 已提交
57 58 59 60 61 62 63 64 65
  ```js
  import distributedObject from '@ohos.data.distributedDataObject'
  var sessionId = distributedObject.genSessionId();
  ```


## DistributedObject

Represents a **distributedObject** instance.
A
annie_wangli 已提交
66

A
annie_wangli 已提交
67 68 69 70
### setSessionId

setSessionId(sessionId?: string): boolean

A
annie_wangli 已提交
71
Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network.
A
annie_wangli 已提交
72

A
annie_wangli 已提交
73 74 75
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
A
annie_wangli 已提交
76 77 78

  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
annie_wangli 已提交
79 80 81 82 83 84 85
  | 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;<br>returns **false** otherwise. |
A
Annie_wang 已提交
86

A
annie_wangli 已提交
87 88
**Example**

A
annie_wangli 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  ```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"}});
  // Add g_object to the distributed network.
  g_object.setSessionId(distributedObject.genSessionId());
  // Remove g_object from the distributed network.
  g_object.setSessionId("");
  ```


### on('change')

on(type: 'change', callback: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void

A
annie_wangli 已提交
104
Subscribes to the data changes of this distributed data object.
A
annie_wangli 已提交
105

A
annie_wangli 已提交
106
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
107

A
annie_wangli 已提交
108
**Parameters**
A
annie_wangli 已提交
109 110 111
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data change events.|
A
annie_wangli 已提交
112
  | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | Yes| Callback used to return the changes of the distributed data object.<br>**sessionId** indicates the session ID of the distributed data object.<br>**fields** indicates the attributes of the distributed data object changed.|
A
annie_wangli 已提交
113

A
annie_wangli 已提交
114
**Example**
A
annie_wangli 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  ```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]);
          });
      }
  });
  ```

### off('change')

off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array&lt;string&gt; }>): void

A
annie_wangli 已提交
133
Unsubscribes from the data changes of this distributed data object.
A
annie_wangli 已提交
134

A
annie_wangli 已提交
135
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
136

A
annie_wangli 已提交
137
**Parameters**
A
annie_wangli 已提交
138 139
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
annie_wangli 已提交
140 141
  | type | string | Yes| Event type to subscribe to. The value is **change**, which indicates data change events.|
  | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | No| 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.<br>**sessionId** indicates the session ID of the distributed data object.<br>**fields** indicates the attributes of the distributed data object changed.|
A
annie_wangli 已提交
142 143


A
annie_wangli 已提交
144
**Example**
A
annie_wangli 已提交
145 146 147 148 149 150 151
  ```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);
  });
A
annie_wangli 已提交
152
  // Unsubscribe from the data change callback for the specified distributed data object.
A
annie_wangli 已提交
153 154 155 156 157 158 159 160 161 162 163
  g_object.off("change", function (sessionId, changeData) {
      console.info("change" + sessionId);
  });
  // Unsubscribe from all data change callbacks.
  g_object.off("change");
  ```

### on('status')

on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, status: 'online' | 'offline' }>): void

A
annie_wangli 已提交
164
Subscribes to the status changes (online or offline) of this distributed data object.
A
annie_wangli 已提交
165

A
annie_wangli 已提交
166
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
167

A
annie_wangli 已提交
168
**Parameters**
A
annie_wangli 已提交
169 170 171
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.|
A
annie_wangli 已提交
172 173 174
  | callback | Callback<{ sessionId: string, networkId: string, status: 'online' \| 'offline' }> | Yes| Callback used to return the online or offline status.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** indicates the network ID of the device.<br>**status** indicates the status, which can be online or offline.|

**Example**
A
annie_wangli 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188
  ```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;
  });
  ```

### off('status')

off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, status: 'online' | 'offline' }>): void


A
annie_wangli 已提交
189
Unsubscribes from the status (online or offline) changes of the distributed data object. 
A
annie_wangli 已提交
190

A
annie_wangli 已提交
191
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
192

A
annie_wangli 已提交
193
**Parameters**
A
annie_wangli 已提交
194 195 196
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | string | Yes| Event type to subscribe to. The value is "status", which indicates the status (online or offline) change events.|
A
annie_wangli 已提交
197
  | callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback used to return the status changes. If this parameter is not specified, all the status change callbacks will be unregistered.<br>**sessionId** indicates the session ID of the distributed data object.<br>**deviceId** indicates the device ID of the distributed data object.<br>**status** indicates the status, which can be online or offline.|
A
annie_wangli 已提交
198 199


A
annie_wangli 已提交
200
**Example**
A
annie_wangli 已提交
201 202 203 204 205
  ```js
  import distributedObject from '@ohos.data.distributedDataObject'  
  g_object.on("status", function (sessionId, networkId, status) {
      this.response += "status changed " + sessionId + " " + status + " " + networkId;
  });
A
annie_wangli 已提交
206
  // Unsubscribe from the status change callback for the specified distributed data object.
A
annie_wangli 已提交
207 208 209 210 211 212
  g_object.off("status", function (sessionId, networkId, status) {
      this.response += "status changed " + sessionId + " " + status + " " + networkId;
  });
  // Unsubscribe from all status change callbacks.
  g_object.off("status");
  ```