js-apis-data-distributedobject.md 27.0 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.data.distributedDataObject (Distributed Data Object)
A
annie_wangli 已提交
2

A
Annie_wang 已提交
3
The **distributedDataObject** module 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 已提交
4

A
Annie_wang 已提交
5
> **NOTE**
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
```

A
Annie_wang 已提交
16
## distributedObject.create<sup>9+</sup>
A
annie_wangli 已提交
17

A
Annie_wang 已提交
18
create(context: Context, source: object): DataObject
A
annie_wangli 已提交
19

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_wang 已提交
25

A
Annie_wang 已提交
26 27 28 29
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | context | Context | Yes| Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-inner-application-uiAbilityContext.md).|
  | source | object | Yes| Attributes of the distributed data object.|
A
Annie_wang 已提交
30

A
Annie_wang 已提交
31
**Return value**
A
Annie_wang 已提交
32

A
Annie_wang 已提交
33 34
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
35
| [DataObject](#dataobject) | Distributed data object created.|
A
annie_wangli 已提交
36 37

**Example**
A
Annie_wang 已提交
38 39 40

FA model:

A
Annie_wang 已提交
41
```js
A
Annie_wang 已提交
42
// Import the module.
A
Annie_wang 已提交
43
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
44 45 46 47 48
import featureAbility from '@ohos.ability.featureAbility';
// Obtain the context.
let context = featureAbility.getContext();
// Create a distributed data object, which contains attributes of the string, number, boolean, and object types.
let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}});
A
Annie_wang 已提交
49
```
A
annie_wangli 已提交
50

A
Annie_wang 已提交
51 52 53 54 55 56 57
Stage model:

```ts
// Import the module.
import distributedObject from '@ohos.data.distributedDataObject';
import UIAbility from '@ohos.app.ability.UIAbility';

A
Annie_wang 已提交
58 59
let g_object = null;

A
Annie_wang 已提交
60 61
class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage){
A
Annie_wang 已提交
62 63
        // Create a distributed data object, which has attributes of the string, number, boolean, and object types.
        g_object = distributedObject.create(this.context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}});
A
Annie_wang 已提交
64 65 66
    }
}
```
A
annie_wangli 已提交
67

A
Annie_wang 已提交
68
## distributedObject.genSessionId
A
annie_wangli 已提交
69 70 71

genSessionId(): string

A
annie_wangli 已提交
72
Creates a random session ID.
A
annie_wangli 已提交
73

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

A
Annie_wang 已提交
76
**Return value**
A
Annie_wang 已提交
77

A
Annie_wang 已提交
78 79 80
  | Type| Description|
  | -------- | -------- |
  | string | Session ID created.|
A
annie_wangli 已提交
81

A
annie_wangli 已提交
82
**Example**
A
Annie_wang 已提交
83

A
Annie_wang 已提交
84 85
```js
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
86
let sessionId = distributedObject.genSessionId();
A
Annie_wang 已提交
87
```
A
annie_wangli 已提交
88

A
Annie_wang 已提交
89 90 91 92 93 94
## SaveSuccessResponse<sup>9+</sup>

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

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

A
Annie_wang 已提交
95 96 97 98 99
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sessionId | string | Yes| Unique ID for multi-device collaboration.|
| version | number | Yes| Version of the distributed data object saved.|
| deviceId | string | Yes| 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.|
A
Annie_wang 已提交
100 101 102 103 104 105 106

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

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

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

A
Annie_wang 已提交
107 108 109
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sessionId | string | Yes| Unique ID for multi-device collaboration.|
A
annie_wangli 已提交
110

A
Annie_wang 已提交
111
## DataObject
A
annie_wangli 已提交
112

A
Annie_wang 已提交
113
Provides APIs for managing a distributed data object. Before using any API of this class, use [create()](#distributedobjectcreate9) to create a **DataObject** object.
A
annie_wangli 已提交
114

A
Annie_wang 已提交
115
### setSessionId<sup>9+</sup>
A
annie_wangli 已提交
116

A
Annie_wang 已提交
117 118 119 120 121 122 123 124 125 126
setSessionId(sessionId: string, callback: AsyncCallback&lt;void&gt;): void

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**

A
Annie_wang 已提交
127 128 129 130
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | sessionId | string | Yes| ID of a distributed data object on a trusted network.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Asynchronous callback invoked when the session ID is successfully set.|
A
Annie_wang 已提交
131 132 133

**Error codes**

A
Annie_wang 已提交
134
  For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md).
A
Annie_wang 已提交
135

A
Annie_wang 已提交
136 137 138
  | ID| Error Message|
  | -------- | -------- |
  | 15400001 | Create table failed.|
A
Annie_wang 已提交
139 140 141 142 143 144

**Example**

```js
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId(), ()=>{
A
Annie_wang 已提交
145
    console.info("join session");
A
Annie_wang 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
});
```

### setSessionId<sup>9+</sup>

setSessionId(callback: AsyncCallback&lt;void&gt;): void

Exits all joined sessions.

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

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

**Parameters**

A
Annie_wang 已提交
161 162 163
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Asynchronous callback invoked when the distributed data object exits all joined sessions.|
A
Annie_wang 已提交
164 165 166

**Error codes**

A
Annie_wang 已提交
167
  For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md).
A
Annie_wang 已提交
168

A
Annie_wang 已提交
169 170 171
  | ID| Error Message|
  | -------- | -------- |
  | 15400001 | Create table failed.|
A
Annie_wang 已提交
172 173 174 175 176 177

**Example**

```js
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId(), ()=>{
A
Annie_wang 已提交
178
    console.info("join session");
A
Annie_wang 已提交
179 180 181
});
// Exit the distributed network.
g_object.setSessionId(() => {
A
Annie_wang 已提交
182
    console.info("leave all lession.");
A
Annie_wang 已提交
183 184 185 186 187 188
});
```

### setSessionId<sup>9+</sup>

setSessionId(sessionId?: string): Promise&lt;void&gt;
A
annie_wangli 已提交
189

A
annie_wangli 已提交
190
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 已提交
191

A
Annie_wang 已提交
192 193
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
194 195 196
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
A
annie_wangli 已提交
197

A
Annie_wang 已提交
198 199 200
  | 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 已提交
201

A
Annie_wang 已提交
202
**Return value**
A
annie_wangli 已提交
203

A
Annie_wang 已提交
204 205
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
206 207 208 209
| Promise&lt;void&gt; | Promise that returns no value.|

**Error codes**

A
Annie_wang 已提交
210
  For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md).
A
Annie_wang 已提交
211

A
Annie_wang 已提交
212 213 214
  | ID| Error Message|
  | -------- | -------- |
  | 15400001 | Create table failed.|
A
Annie_wang 已提交
215

A
annie_wangli 已提交
216 217
**Example**

A
Annie_wang 已提交
218
```js
A
Annie_wang 已提交
219 220 221 222 223 224 225 226
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId()).then (()=>{
    console.info("join session.");
    }).catch((error)=>{
        console.info("error:" + error.code + error.message);
});
// Exit the distributed network.
g_object.setSessionId().then (()=>{
A
Annie_wang 已提交
227
    console.info("leave all lession.");
A
Annie_wang 已提交
228 229 230 231
    }).catch((error)=>{
        console.info("error:" + error.code + error.message);
});
```
A
annie_wangli 已提交
232

A
Annie_wang 已提交
233
### on('change')<sup>9+</sup>
A
annie_wangli 已提交
234 235 236

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

A
Annie_wang 已提交
237
Subscribes to data changes of this distributed data object.
A
annie_wangli 已提交
238

A
annie_wangli 已提交
239
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
240

A
annie_wangli 已提交
241
**Parameters**
A
Annie_wang 已提交
242

A
Annie_wang 已提交
243 244 245 246
  | 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 invoked 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 已提交
247

A
annie_wangli 已提交
248
**Example**
A
Annie_wang 已提交
249

A
Annie_wang 已提交
250 251 252 253 254 255 256 257 258 259 260
```js
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 已提交
261

A
Annie_wang 已提交
262
### off('change')<sup>9+</sup>
A
annie_wangli 已提交
263 264 265

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

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

A
annie_wangli 已提交
268
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
269

A
annie_wangli 已提交
270
**Parameters**
A
Annie_wang 已提交
271

A
Annie_wang 已提交
272 273
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
274
| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.|
A
Annie_wang 已提交
275
  | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | No| Callback for data changes. If this parameter is not specified, all data change callbacks of this distributed data 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 已提交
276 277


A
annie_wangli 已提交
278
**Example**
A
Annie_wang 已提交
279

A
Annie_wang 已提交
280 281 282 283 284 285
```js
// Unregister the specified data change callback.
g_object.off("change", globalThis.changeCallback);
// Unregister all data change callbacks.
g_object.off("change");
```
A
annie_wangli 已提交
286

A
Annie_wang 已提交
287
### on('status')<sup>9+</sup>
A
annie_wangli 已提交
288 289 290

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

A
Annie_wang 已提交
291
Subscribes to status changes of this distributed data object.
A
annie_wangli 已提交
292

A
annie_wangli 已提交
293
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
294

A
annie_wangli 已提交
295
**Parameters**
A
Annie_wang 已提交
296

A
Annie_wang 已提交
297 298 299 300
  | 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 invoked to return the status change.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** indicates the object device ID, that is, **deviceId**.<br>**status** indicates the object status, which can be online or offline.|
A
annie_wangli 已提交
301 302

**Example**
A
Annie_wang 已提交
303

A
Annie_wang 已提交
304
```js
A
Annie_wang 已提交
305 306 307 308 309 310 311
globalThis.statusCallback = (sessionId, networkId, status) => {
    globalThis.response += "status changed " + sessionId + " " + status + " " + networkId;
}
g_object.on("status", globalThis.statusCallback);
```

### off('status')<sup>9+</sup>
A
annie_wangli 已提交
312

A
Annie_wang 已提交
313
off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, status: 'online' | 'offline' }>): void
A
annie_wangli 已提交
314

A
Annie_wang 已提交
315
Unsubscribes from the status change of this distributed data object.
A
annie_wangli 已提交
316

A
annie_wangli 已提交
317
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
318

A
annie_wangli 已提交
319
**Parameters**
A
Annie_wang 已提交
320

A
Annie_wang 已提交
321 322
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
323
| 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.|
A
Annie_wang 已提交
324
  | callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback for status changes. If this parameter is not specified, all status change callbacks of this distributed data object will be unsubscribed from.<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 object status, which can be online or offline.|
A
annie_wangli 已提交
325 326


A
annie_wangli 已提交
327
**Example**
A
Annie_wang 已提交
328

A
Annie_wang 已提交
329 330 331 332
```js
globalThis.statusCallback = (sessionId, networkId, status) => {
    globalThis.response += "status changed " + sessionId + " " + status + " " + networkId;
}
A
Annie_wang 已提交
333
// Unregister the specified status change callback.
A
Annie_wang 已提交
334
g_object.off("status",globalThis.statusCallback);
A
Annie_wang 已提交
335
// Unregister all status change callbacks.
A
Annie_wang 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
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**
A
Annie_wang 已提交
356

A
Annie_wang 已提交
357 358 359 360
  | 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](#savesuccessresponse9)&gt; | Yes| Callback invoked to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.|
A
Annie_wang 已提交
361 362

**Example**
A
Annie_wang 已提交
363 364 365

```ts
g_object.setSessionId("123456");
A
Annie_wang 已提交
366 367 368 369 370 371
g_object.save("local", (err, result) => {
    if (err) {
        console.info("save failed, error code = " + err.code);
        console.info("save failed, error message: " + err.message);
        return;
    }
A
Annie_wang 已提交
372
    console.info("save callback");
A
Annie_wang 已提交
373 374 375 376 377
    console.info("save sessionId: " + result.sessionId);
    console.info("save version: " + result.version);
    console.info("save deviceId:  " + result.deviceId);
});
```
A
Annie_wang 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

### 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**
A
Annie_wang 已提交
396

A
Annie_wang 已提交
397 398 399
  | 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. |
A
Annie_wang 已提交
400

A
Annie_wang 已提交
401
**Return value**
A
Annie_wang 已提交
402

A
Annie_wang 已提交
403 404 405
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[SaveSuccessResponse](#savesuccessresponse9)&gt; | Promise used to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.|
A
Annie_wang 已提交
406 407 408

**Example**

A
Annie_wang 已提交
409 410
```js
g_object.setSessionId("123456");
A
Annie_wang 已提交
411
g_object.save("local").then((result) => {
A
Annie_wang 已提交
412
    console.info("save callback");
A
Annie_wang 已提交
413 414 415
    console.info("save sessionId " + result.sessionId);
    console.info("save version " + result.version);
    console.info("save deviceId " + result.deviceId);
A
Annie_wang 已提交
416 417 418
}).catch((err) => {
    console.info("save failed, error code = " + err.code);
    console.info("save failed, error message: " + err.message);
A
Annie_wang 已提交
419 420
});
```
A
Annie_wang 已提交
421 422 423

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

A
Annie_wang 已提交
424
revokeSave(callback: AsyncCallback&lt;RevokeSaveSuccessResponse&gt;): void
A
Annie_wang 已提交
425

A
Annie_wang 已提交
426
Revokes the data of this distributed data object saved. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
427 428 429 430 431 432 433

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**
A
Annie_wang 已提交
434

A
Annie_wang 已提交
435 436 437
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)&gt; | Yes| Callback invoked to return **RevokeSaveSuccessResponse**, which contains the session ID.|
A
Annie_wang 已提交
438 439 440

**Example**

A
Annie_wang 已提交
441
```js
A
Annie_wang 已提交
442 443
g_object.setSessionId("123456");
// Save data for persistence. 
A
Annie_wang 已提交
444 445 446 447 448 449
g_object.save("local", (err, result) => {
    if (err) {
        console.info("save failed, error code = " + err.code);
        console.info("save failed, error message: " + err.message);
        return;
    }
A
Annie_wang 已提交
450
    console.info("save callback");
A
Annie_wang 已提交
451 452 453
    console.info("save sessionId: " + result.sessionId);
    console.info("save version: " + result.version);
    console.info("save deviceId:  " + result.deviceId);
A
Annie_wang 已提交
454 455
});
// Delete the persistence data.
A
Annie_wang 已提交
456 457 458 459 460 461 462 463
g_object.revokeSave((err, result) => {
    if (err) {
      console.info("revokeSave failed, error code = " + err.code);
      console.info("revokeSave failed, error message: " + err.message);
      return;
    }
    console.info("revokeSave callback");
    console.info("revokeSave sessionId " + result.sessionId);
A
Annie_wang 已提交
464 465
});
```
A
Annie_wang 已提交
466 467 468

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

A
Annie_wang 已提交
469
revokeSave(): Promise&lt;RevokeSaveSuccessResponse&gt;
A
Annie_wang 已提交
470

A
Annie_wang 已提交
471
Revokes the data of this distributed data object saved. This API uses a promise to return the result.
A
Annie_wang 已提交
472 473 474 475 476 477

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

A
Annie_wang 已提交
478
**Return value**
A
Annie_wang 已提交
479

A
Annie_wang 已提交
480 481 482
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)&gt; | Promise used to return **RevokeSaveSuccessResponse**, which contains the session ID.|
A
Annie_wang 已提交
483 484 485

**Example**

A
Annie_wang 已提交
486 487 488 489
```ts
g_object.setSessionId("123456");
// Save data for persistence.
g_object.save("local").then((result) => {
A
Annie_wang 已提交
490
    console.info("save callback");
A
Annie_wang 已提交
491 492 493
    console.info("save sessionId " + result.sessionId);
    console.info("save version " + result.version);
    console.info("save deviceId " + result.deviceId);
A
Annie_wang 已提交
494 495 496
}).catch((err) => {
    console.info("save failed, error code = " + err.code);
    console.info("save failed, error message: " + err.message);
A
Annie_wang 已提交
497 498 499
});
// Delete the persistence data.
g_object.revokeSave().then((result) => {
A
Annie_wang 已提交
500 501
    console.info("revokeSave callback");
    console.info("sessionId" + result.sessionId);
A
Annie_wang 已提交
502 503 504
}).catch((err)=> {
    console.info("revokeSave failed, error code = " + err.code);
    console.info("revokeSave failed, error message = " + err.message);
A
Annie_wang 已提交
505 506
});
```
A
Annie_wang 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

## distributedObject.createDistributedObject<sup>(deprecated)</sup>

createDistributedObject(source: object): DistributedObject


Creates a distributed data object.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use **distributedObject.create**.

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

**Parameters**

A
Annie_wang 已提交
523 524 525
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | source | object | Yes| Attributes of the distributed data object.|
A
Annie_wang 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

**Return value**

| Type| Description|
| -------- | -------- |
| [DistributedObject](#distributedobjectdeprecated) | Distributed data object created.|

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
// Create a distributed data object, which contains attributes of the string, number, boolean, and object types.
let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}});
```

## DistributedObject<sup>(deprecated)</sup>

A
Annie_wang 已提交
543
Provides APIs for managing a distributed data object. Before using any API of this class, use [createDistributedObject()](#distributedobjectcreatedistributedobjectdeprecated) to create a **DistributedObject** object.
A
Annie_wang 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

### setSessionId<sup>(deprecated)</sup>

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.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [setSessionId](#setsessionid9).

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

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

**Parameters**

A
Annie_wang 已提交
561 562 563
  | 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_wang 已提交
564 565 566

**Return value**

A
Annie_wang 已提交
567 568 569
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the session ID is set successfully;<br>returns **false** otherwise. |
A
Annie_wang 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
let 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')<sup>(deprecated)</sup>

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

Subscribes to data changes of this distributed data object.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [on('change')](#onchange9).

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

**Parameters**

A
Annie_wang 已提交
596 597 598 599
  | 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 invoked 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_wang 已提交
600 601 602 603

**Example**

```js
A
Annie_wang 已提交
604
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
let 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')<sup>(deprecated)</sup>

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

Unsubscribes from the data changes of this distributed data object.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [off('change')](#offchange9).

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

**Parameters**

A
Annie_wang 已提交
631 632
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
633
| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.|
A
Annie_wang 已提交
634
  | callback | Callback<{ sessionId: string, fields: Array&lt;string&gt; }> | No| Callback for data changes. If this parameter is not specified, all data change callbacks of this distributed data 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_wang 已提交
635 636 637 638 639


**Example**

```js
A
Annie_wang 已提交
640
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
let 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')<sup>(deprecated)</sup>

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

Subscribes to status changes of this distributed data object.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [on('status')](#onstatus9).

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

**Parameters**

A
Annie_wang 已提交
662 663 664 665
  | 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 invoked to return the status change.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** indicates the object device ID, that is, **deviceId**.<br>**status** indicates the object status, which can be online or offline.|
A
Annie_wang 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
globalThis.statusCallback = (sessionId, networkId, status) => {
    globalThis.response += "status changed " + sessionId + " " + status + " " + networkId;
}
let 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')<sup>(deprecated)</sup>

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

Unsubscribes from the status change of this distributed data object.

> **NOTE**
>
A
Annie_wang 已提交
686
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [off('status')](#offstatus9).
A
Annie_wang 已提交
687 688 689 690 691

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

**Parameters**

A
Annie_wang 已提交
692 693
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
694
| 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.|
A
Annie_wang 已提交
695 696 697 698 699 700
| callback | Callback<{ sessionId: string, deviceId: string, status: 'online' \| 'offline' }> | No| Callback for status changes. If this parameter is not specified, all status change callbacks of this distributed data object 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 object status, which can be online or offline.|


**Example**

```js
A
Annie_wang 已提交
701
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
702 703 704 705 706 707 708 709 710
let 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;
}
// Unregister the specified status change callback.
g_object.off("status",globalThis.statusCallback);
// Unregister all status change callbacks.
g_object.off("status");
```