js-apis-data-distributedobject.md 14.7 KB
Newer Older
A
annie_wangli 已提交
1
# Distributed Data Object
A
annie_wangli 已提交
2

A
Annie_wang 已提交
3 4
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.

A
Annie_wang 已提交
5
> **NOTE**<br/>
A
Annie_wang 已提交
6
>
A
annie_wangli 已提交
7 8 9 10 11 12
> 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 已提交
13
import distributedObject from '@ohos.data.distributedDataObject';
A
annie_wangli 已提交
14 15 16 17 18 19 20
```

## distributedDataObject.createDistributedObject

createDistributedObject(source: object): DistributedObject


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

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

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

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

**Example**
A
annie_wangli 已提交
36
  ```js
A
Annie_wang 已提交
37
  import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
38
  // Create a distributed data object, which contains attributes of four types, namely, string, number, boolean, and object.
A
annie_wangli 已提交
39 40 41 42 43
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, 
                 parent:{mother:"jack mom",father:"jack Dad"}});
  ```


A
Annie_wang 已提交
44
## distributedObject.genSessionId
A
annie_wangli 已提交
45 46 47

genSessionId(): string

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

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

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

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

A
Annie_wang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
## SaveSuccessResponse<sup>9+</sup>

Called when the **Save()** API is successfully called.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

| Name| Type| Description|
| -------- | -------- | -------- |
| sessionId | string | Unique ID for multi-device collaboration.|
| version | number |Version of the distributed data object saved.|
| deviceId | string | ID of the device where the distributed data object is stored. The default value is **local**, which identifies a local device. You can set it as required.|

## RevokeSaveSuccessResponse<sup>9+</sup>

Called when the **revokeSave()** API is successfully called.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

| Name| Type| Description|
| -------- | -------- | -------- |
| sessionId | string | Unique ID for multi-device collaboration.|
A
annie_wangli 已提交
84 85 86

## DistributedObject

A
Annie_wang 已提交
87
Represents a distributed data object.
A
annie_wangli 已提交
88

A
annie_wangli 已提交
89 90 91 92
### setSessionId

setSessionId(sessionId?: string): boolean

A
annie_wangli 已提交
93
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 已提交
94

A
Annie_wang 已提交
95 96
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
97 98 99
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
A
annie_wangli 已提交
100

A
Annie_wang 已提交
101 102 103
| 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.|
A
annie_wangli 已提交
104

A
Annie_wang 已提交
105
**Return value**
A
annie_wangli 已提交
106

A
Annie_wang 已提交
107 108 109
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the session ID is set successfully;<br>returns **false** otherwise. |
A
Annie_wang 已提交
110

A
annie_wangli 已提交
111 112
**Example**

A
annie_wangli 已提交
113
  ```js
A
Annie_wang 已提交
114
  import distributedObject from '@ohos.data.distributedDataObject';
A
annie_wangli 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127
  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_wang 已提交
128
Subscribes to the changes of this distributed data object.
A
annie_wangli 已提交
129

A
annie_wangli 已提交
130
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
131

A
annie_wangli 已提交
132
**Parameters**
A
Annie_wang 已提交
133 134 135 136
| 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&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 changed attributes of the distributed data object.|
A
annie_wangli 已提交
137

A
annie_wangli 已提交
138
**Example**
A
Annie_wang 已提交
139 140 141 142 143 144 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"}});
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);
```
A
annie_wangli 已提交
152 153 154 155 156

### off('change')

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

A
Annie_wang 已提交
157
Unsubscribes from the changes of this distributed data object.
A
annie_wangli 已提交
158

A
annie_wangli 已提交
159
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
160

A
annie_wangli 已提交
161
**Parameters**
A
Annie_wang 已提交
162 163 164 165
| 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&lt;string&gt; }> | No| Callback to be unregistered. If this parameter is not set, all data change callbacks of the object will be unregistered.<br>**sessionId** indicates the session ID of the distributed data object.<br>**fields** indicates the changed attributes of the distributed data object.|
A
annie_wangli 已提交
166 167


A
annie_wangli 已提交
168
**Example**
A
Annie_wang 已提交
169 170 171 172 173 174 175 176
```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");
```
A
annie_wangli 已提交
177 178 179 180 181

### on('status')

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

A
Annie_wang 已提交
182
Subscribes to the status change (online or offline) of this distributed data object.
A
annie_wangli 已提交
183

A
annie_wangli 已提交
184
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
185

A
annie_wangli 已提交
186
**Parameters**
A
Annie_wang 已提交
187 188 189 190
| 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.<br>**sessionId**: session ID of the distributed data object.<br>**networkId**: object device ID, that is, **deviceId**.<br>**status** indicates the object status, which can be online or offline. |
A
annie_wangli 已提交
191 192

**Example**
A
Annie_wang 已提交
193 194 195 196 197 198 199 200
```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);
```
A
annie_wangli 已提交
201 202 203 204 205 206

### off('status')

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


A
Annie_wang 已提交
207
Unsubscribes from the status change (online or offline) of this distributed data object.
A
annie_wangli 已提交
208

A
annie_wangli 已提交
209
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
210

A
annie_wangli 已提交
211
**Parameters**
A
Annie_wang 已提交
212 213 214 215
| 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.<br>**sessionId**: 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 已提交
216 217


A
annie_wangli 已提交
218
**Example**
A
Annie_wang 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
```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");
```

### save<sup>9+</sup>

save(deviceId: string, callback: AsyncCallback&lt;SaveSuccessResponse&gt;): void

Saves a distributed data object. This API uses an asynchronous callback to return the result.

If the application is active, the saved data will not be released. When the application exits and restarts, the data saved on the device will be restored.

The saved data will be released in the following cases:

- The data is stored for more than 24 hours.
- The application has been uninstalled.
- Data is successfully restored.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceId | string | Yes| ID of the device where data is stored. The value **local** indicates the local device.|
| callback | AsyncCallback&lt;[SaveSuccessResponse](#savesuccessresponse)&gt; | Yes| Callback used to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.|

**Example**

  ```ts
  import distributedObject from '@ohos.data.distributedDataObject';
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false});
  g_object.setSessionId("123456");
  g_object.save("local", (result)=>{
      console.log("save callback");
      console.info("save sessionId " + result.sessionId);
      console.info("save version " + result.version);
      console.info("save deviceId " + result.deviceId);
  });
  ```

### save<sup>9+</sup>

save(deviceId: string): Promise&lt;SaveSuccessResponse&gt;

Saves a distributed data object. This API uses a promise to return the result.

If the application is active, the saved data will not be released. When the application exits and restarts, the data saved on the device will be restored.

The saved data will be released in the following cases:

- The data is stored for more than 24 hours.
- The application has been uninstalled.
- Data is successfully restored.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceId | string | Yes| ID of the device where the data is saved. The default value is **local**, which indicates the local device. |

  **Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;[SaveSuccessResponse](#savesuccessresponse)&gt; | Promise used to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.|

**Example**

  ```ts
  import distributedObject from '@ohos.data.distributedDataObject';
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false});
  g_object.setSessionId("123456");
  g_object.save("local").then((result)=>{
      console.log("save callback");
      console.info("save sessionId " + result.sessionId);
      console.info("save version " + result.version);
      console.info("save deviceId " + result.deviceId);
  }, ()=>{
      console.error("save failed");
  });
  ```

### revokeSave<sup>9+</sup>

revokeSave(callback: AsyncCallback&lt;SaveSuccessResponse&gt;): void

Revokes the saving operation of a distributed data object. This API uses an asynchronous callback to return the result.

If the object is saved on the local device, the data saved on all trusted devices will be deleted.
If the object is stored on another device, the data on the local device will be deleted.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse)&gt; | No| Callback used to return **RevokeSaveSuccessResponse**, which contains the session ID.|

**Example**

  ```ts
  import distributedObject from '@ohos.data.distributedDataObject';
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false});
  g_object.setSessionId("123456");
  g_object.revokeSave((result, data) =>{
    console.log("revokeSave callback");
A
annie_wangli 已提交
334
  });
A
Annie_wang 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  ```

### revokeSave<sup>9+</sup>

revokeSave(): Promise&lt;SaveSuccessResponse&gt;

Revokes the saving operation of a distributed data object. This API uses a promise to return the result.

If the object is saved on the local device, the data saved on all trusted devices will be deleted.
If the object is stored on another device, the data on the local device will be deleted.

**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

  **Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse)&gt; | Promise used to return **RevokeSaveSuccessResponse**, which contains the session ID.|

**Example**

  ```ts
  import distributedObject from '@ohos.data.distributedDataObject';
  var g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false});
  g_object.setSessionId("123456");
  g_object.revokeSave("local").then((result)=>{
      console.log("revokeSave callback");
      console.log("sessionId" + result.sessionId);
  }, ()=>{
      console.error("revokeSave failed");
A
annie_wangli 已提交
365 366
  });
  ```