js-apis-data-distributedobject.md 29.4 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
import featureAbility from '@ohos.ability.featureAbility';
A
Annie_wang 已提交
45
import { BusinessError } from '@ohos.base';
A
Annie_wang 已提交
46 47
// Obtain the context.
let context = featureAbility.getContext();
A
Annie_wang 已提交
48 49 50 51 52 53 54 55 56 57 58
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DataObject = distributedObject.create(context, source);
A
Annie_wang 已提交
59
```
A
annie_wangli 已提交
60

A
Annie_wang 已提交
61 62 63 64 65 66
Stage model:

```ts
// Import the module.
import distributedObject from '@ohos.data.distributedDataObject';
import UIAbility from '@ohos.app.ability.UIAbility';
A
Annie_wang 已提交
67 68 69 70 71 72 73 74 75
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

let g_object: distributedObject.DataObject = null;
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
A
Annie_wang 已提交
76
class EntryAbility extends UIAbility {
A
Annie_wang 已提交
77 78 79 80 81 82 83
    onWindowStageCreate(windowStage: window.WindowStage) {
        let source: sourceObject = {
            name: "amy",
            age:18,
            isVis:false
        }
        g_object = distributedObject.create(this.context, source);
A
Annie_wang 已提交
84 85 86
    }
}
```
A
annie_wangli 已提交
87

A
Annie_wang 已提交
88
## distributedObject.genSessionId
A
annie_wangli 已提交
89 90 91

genSessionId(): string

A
annie_wangli 已提交
92
Creates a random session ID.
A
annie_wangli 已提交
93

A
annie_wangli 已提交
94
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
95

A
Annie_wang 已提交
96
**Return value**
A
Annie_wang 已提交
97

A
Annie_wang 已提交
98 99 100
| Type| Description|
| -------- | -------- |
| string | Session ID created.|
A
annie_wangli 已提交
101

A
annie_wangli 已提交
102
**Example**
A
Annie_wang 已提交
103

A
Annie_wang 已提交
104 105
```js
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
106
let sessionId: string = distributedObject.genSessionId();
A
Annie_wang 已提交
107
```
A
annie_wangli 已提交
108

A
Annie_wang 已提交
109 110 111 112 113 114
## SaveSuccessResponse<sup>9+</sup>

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

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

A
Annie_wang 已提交
115 116 117 118 119
| 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 已提交
120 121 122 123 124 125 126

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

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

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

A
Annie_wang 已提交
127 128 129
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sessionId | string | Yes| Unique ID for multi-device collaboration.|
A
annie_wangli 已提交
130

A
Annie_wang 已提交
131
## DataObject
A
annie_wangli 已提交
132

A
Annie_wang 已提交
133
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 已提交
134

A
Annie_wang 已提交
135
### setSessionId<sup>9+</sup>
A
annie_wangli 已提交
136

A
Annie_wang 已提交
137 138 139 140 141 142 143 144 145 146
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 已提交
147 148 149 150
| 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 已提交
151 152 153

**Error codes**

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

A
Annie_wang 已提交
156 157 158
| ID| Error Message|
| -------- | -------- |
| 15400001 | Create table failed.|
A
Annie_wang 已提交
159 160 161 162 163 164

**Example**

```js
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId(), ()=>{
A
Annie_wang 已提交
165
    console.info("join session");
A
Annie_wang 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
});
```

### 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 已提交
181 182 183
| 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 已提交
184 185 186

**Error codes**

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

A
Annie_wang 已提交
189 190 191
| ID| Error Message|
| -------- | -------- |
| 15400001 | Create table failed.|
A
Annie_wang 已提交
192 193 194 195 196 197

**Example**

```js
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId(), ()=>{
A
Annie_wang 已提交
198
    console.info("join session");
A
Annie_wang 已提交
199 200 201
});
// Exit the distributed network.
g_object.setSessionId(() => {
A
Annie_wang 已提交
202
    console.info("leave all session.");
A
Annie_wang 已提交
203 204 205 206 207 208
});
```

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

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

A
annie_wangli 已提交
210
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 已提交
211

A
Annie_wang 已提交
212 213
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
214 215 216
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject

**Parameters**
A
annie_wangli 已提交
217

A
Annie_wang 已提交
218 219 220
| 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 已提交
221

A
Annie_wang 已提交
222
**Return value**
A
annie_wangli 已提交
223

A
Annie_wang 已提交
224 225
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
226 227 228 229
| Promise&lt;void&gt; | Promise that returns no value.|

**Error codes**

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

A
Annie_wang 已提交
232 233 234
| ID| Error Message|
| -------- | -------- |
| 15400001 | Create table failed.|
A
Annie_wang 已提交
235

A
annie_wangli 已提交
236 237
**Example**

A
Annie_wang 已提交
238
```js
A
Annie_wang 已提交
239 240 241
// Add g_object to the distributed network.
g_object.setSessionId(distributedObject.genSessionId()).then (()=>{
    console.info("join session.");
A
Annie_wang 已提交
242
    }).catch((error: BusinessError)=>{
A
Annie_wang 已提交
243 244 245 246
        console.info("error:" + error.code + error.message);
});
// Exit the distributed network.
g_object.setSessionId().then (()=>{
A
Annie_wang 已提交
247 248
    console.info("leave all session.");
    }).catch((error: BusinessError)=>{
A
Annie_wang 已提交
249 250 251
        console.info("error:" + error.code + error.message);
});
```
A
annie_wangli 已提交
252

A
Annie_wang 已提交
253
### on('change')<sup>9+</sup>
A
annie_wangli 已提交
254 255 256

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

A
Annie_wang 已提交
257
Subscribes to data changes of this distributed data object.
A
annie_wangli 已提交
258

A
annie_wangli 已提交
259
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
260

A
annie_wangli 已提交
261
**Parameters**
A
Annie_wang 已提交
262

A
Annie_wang 已提交
263 264 265 266
| 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 已提交
267

A
annie_wangli 已提交
268
**Example**
A
Annie_wang 已提交
269

A
Annie_wang 已提交
270
```js
A
Annie_wang 已提交
271 272 273
interface ChangeCallback {
  sessionId: string,
  fields: Array<string>
A
Annie_wang 已提交
274
}
A
Annie_wang 已提交
275 276 277 278 279 280 281 282
g_object.on("change", (changeData: ChangeCallback) => {
    console.info("change" + changeData.sessionId);
    if (changeData.fields != null && changeData.fields != undefined) {
        for (let index: number = 0; index < changeData.fields.length; index++) {
            console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]);
        }
    }
});
A
Annie_wang 已提交
283
```
A
annie_wangli 已提交
284

A
Annie_wang 已提交
285
### off('change')<sup>9+</sup>
A
annie_wangli 已提交
286 287 288

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

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

A
annie_wangli 已提交
291
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
292

A
annie_wangli 已提交
293
**Parameters**
A
Annie_wang 已提交
294

A
Annie_wang 已提交
295 296
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
297
| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.|
A
Annie_wang 已提交
298
| 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 已提交
299 300


A
annie_wangli 已提交
301
**Example**
A
Annie_wang 已提交
302

A
Annie_wang 已提交
303 304
```js
// Unregister the specified data change callback.
A
Annie_wang 已提交
305 306 307 308 309 310 311 312
g_object.off("change", (changeData:ChangeCallback) => {
    console.info("change" + changeData.sessionId);
    if (changeData.fields != null && changeData.fields != undefined) {
        for (let index: number = 0; index < changeData.fields.length; index++) {
            console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]);
        }
    }
});
A
Annie_wang 已提交
313 314 315
// Unregister all data change callbacks.
g_object.off("change");
```
A
annie_wangli 已提交
316

A
Annie_wang 已提交
317
### on('status')<sup>9+</sup>
A
annie_wangli 已提交
318 319 320

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

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

A
annie_wangli 已提交
323
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
324

A
annie_wangli 已提交
325
**Parameters**
A
Annie_wang 已提交
326

A
Annie_wang 已提交
327 328 329 330
| 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 已提交
331 332

**Example**
A
Annie_wang 已提交
333

A
Annie_wang 已提交
334
```js
A
Annie_wang 已提交
335 336 337 338
interface onStatusCallback {
    sessionId: string,
    networkId: string,
    status: 'online' | 'offline'
A
Annie_wang 已提交
339
}
A
Annie_wang 已提交
340 341 342 343

g_object.on("status", (statusCallback:onStatusCallback) => {
    console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId);
});
A
Annie_wang 已提交
344 345 346
```

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

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

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

A
annie_wangli 已提交
352
**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject
A
annie_wangli 已提交
353

A
annie_wangli 已提交
354
**Parameters**
A
Annie_wang 已提交
355

A
Annie_wang 已提交
356 357
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
358
| 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 已提交
359
| 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 已提交
360 361


A
annie_wangli 已提交
362
**Example**
A
Annie_wang 已提交
363

A
Annie_wang 已提交
364
```js
A
Annie_wang 已提交
365 366 367 368
interface offStatusCallback {
    sessionId: string,
    networkId: string,
    status: 'online' | 'offline'
A
Annie_wang 已提交
369
}
A
Annie_wang 已提交
370
// Unregister the specified status change callback.
A
Annie_wang 已提交
371 372 373
g_object.off("status", (statusCallback:StatusCallback) => {
    console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId);
});
A
Annie_wang 已提交
374
// Unregister all status change callbacks.
A
Annie_wang 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
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 已提交
395

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

**Example**
A
Annie_wang 已提交
402 403 404

```ts
g_object.setSessionId("123456");
A
Annie_wang 已提交
405
g_object.save("local", (err: BusinessError, result:distributedObject.SaveSuccessResponse) => {
A
Annie_wang 已提交
406 407 408 409 410
    if (err) {
        console.info("save failed, error code = " + err.code);
        console.info("save failed, error message: " + err.message);
        return;
    }
A
Annie_wang 已提交
411
    console.info("save callback");
A
Annie_wang 已提交
412 413 414 415 416
    console.info("save sessionId: " + result.sessionId);
    console.info("save version: " + result.version);
    console.info("save deviceId:  " + result.deviceId);
});
```
A
Annie_wang 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

### 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 已提交
435

A
Annie_wang 已提交
436 437 438
| 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 已提交
439

A
Annie_wang 已提交
440
**Return value**
A
Annie_wang 已提交
441

A
Annie_wang 已提交
442 443 444
| 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 已提交
445 446 447

**Example**

A
Annie_wang 已提交
448 449
```js
g_object.setSessionId("123456");
A
Annie_wang 已提交
450
g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => {
A
Annie_wang 已提交
451
    console.info("save callback");
A
Annie_wang 已提交
452 453 454
    console.info("save sessionId " + result.sessionId);
    console.info("save version " + result.version);
    console.info("save deviceId " + result.deviceId);
A
Annie_wang 已提交
455
}).catch((err: BusinessError) => {
A
Annie_wang 已提交
456 457
    console.info("save failed, error code = " + err.code);
    console.info("save failed, error message: " + err.message);
A
Annie_wang 已提交
458 459
});
```
A
Annie_wang 已提交
460 461 462

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

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

A
Annie_wang 已提交
465
Revokes the data of this distributed data object saved. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
466 467 468 469 470 471 472

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 已提交
473

A
Annie_wang 已提交
474 475 476
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)&gt; | Yes| Callback invoked to return **RevokeSaveSuccessResponse**, which contains the session ID.|
A
Annie_wang 已提交
477 478 479

**Example**

A
Annie_wang 已提交
480
```js
A
Annie_wang 已提交
481 482
g_object.setSessionId("123456");
// Save data for persistence. 
A
Annie_wang 已提交
483
g_object.save("local", (err: BusinessError, result: distributedObject.SaveSuccessResponse) => {
A
Annie_wang 已提交
484 485 486 487 488
    if (err) {
        console.info("save failed, error code = " + err.code);
        console.info("save failed, error message: " + err.message);
        return;
    }
A
Annie_wang 已提交
489
    console.info("save callback");
A
Annie_wang 已提交
490 491 492
    console.info("save sessionId: " + result.sessionId);
    console.info("save version: " + result.version);
    console.info("save deviceId:  " + result.deviceId);
A
Annie_wang 已提交
493 494
});
// Delete the persistence data.
A
Annie_wang 已提交
495
g_object.revokeSave((err: BusinessError, result: distributedObject.RevokeSaveSuccessResponse) => {
A
Annie_wang 已提交
496 497 498 499 500 501 502
    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 已提交
503 504
});
```
A
Annie_wang 已提交
505 506 507

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

A
Annie_wang 已提交
508
revokeSave(): Promise&lt;RevokeSaveSuccessResponse&gt;
A
Annie_wang 已提交
509

A
Annie_wang 已提交
510
Revokes the data of this distributed data object saved. This API uses a promise to return the result.
A
Annie_wang 已提交
511 512 513 514 515 516

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 已提交
517
**Return value**
A
Annie_wang 已提交
518

A
Annie_wang 已提交
519 520 521
| Type| Description|
| -------- | -------- |
| Promise&lt;[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)&gt; | Promise used to return **RevokeSaveSuccessResponse**, which contains the session ID.|
A
Annie_wang 已提交
522 523 524

**Example**

A
Annie_wang 已提交
525 526
```ts
g_object.setSessionId("123456");
A
Annie_wang 已提交
527 528
// Save data for persistence. 
g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => {
A
Annie_wang 已提交
529
    console.info("save callback");
A
Annie_wang 已提交
530 531 532
    console.info("save sessionId " + result.sessionId);
    console.info("save version " + result.version);
    console.info("save deviceId " + result.deviceId);
A
Annie_wang 已提交
533
}).catch((err: BusinessError) => {
A
Annie_wang 已提交
534 535
    console.info("save failed, error code = " + err.code);
    console.info("save failed, error message: " + err.message);
A
Annie_wang 已提交
536 537
});
// Delete the persistence data.
A
Annie_wang 已提交
538
g_object.revokeSave().then((result: distributedObject.RevokeSaveSuccessResponse) => {
A
Annie_wang 已提交
539 540
    console.info("revokeSave callback");
    console.info("sessionId" + result.sessionId);
A
Annie_wang 已提交
541
}).catch((err: BusinessError)=> {
A
Annie_wang 已提交
542 543
    console.info("revokeSave failed, error code = " + err.code);
    console.info("revokeSave failed, error message = " + err.message);
A
Annie_wang 已提交
544 545
});
```
A
Annie_wang 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

## 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 已提交
562 563 564
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| source | object | Yes| Attributes of the distributed data object.|
A
Annie_wang 已提交
565 566 567 568 569 570 571 572 573 574 575

**Return value**

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

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
576 577 578 579 580 581 582 583 584 585 586
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
A
Annie_wang 已提交
587 588 589 590
```

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

A
Annie_wang 已提交
591
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 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

### 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 已提交
609 610 611
| 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 已提交
612 613 614

**Return value**

A
Annie_wang 已提交
615 616 617
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the session ID is set successfully;<br>returns **false** otherwise. |
A
Annie_wang 已提交
618 619 620 621 622

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
623 624 625 626 627 628 629 630 631 632 633
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
A
Annie_wang 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
// 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 已提交
654 655 656 657
| 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 已提交
658 659 660 661

**Example**

```js
A
Annie_wang 已提交
662
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
663 664 665 666 667 668 669 670
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
interface ChangeCallback {
    sessionId: string,
    fields: Array<string>
A
Annie_wang 已提交
671
}
A
Annie_wang 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
g_object.on("change", (changeData:ChangeCallback) => {
    console.info("change" + changeData.sessionId);
    if (changeData.fields != null && changeData.fields != undefined) {
        for (let index: number = 0; index < changeData.fields.length; index++) {
            console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]);
        }
    }
});
A
Annie_wang 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
```

### 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 已提交
702 703
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
704
| type | string | Yes| Event type to unsubscribe from. The value is **change**, which indicates data changes.|
A
Annie_wang 已提交
705
| 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 已提交
706 707 708 709 710


**Example**

```js
A
Annie_wang 已提交
711
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
interface ChangeCallback {
    sessionId: string,
    fields: Array<string>
}
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
A
Annie_wang 已提交
727
// Unregister the specified data change callback.
A
Annie_wang 已提交
728 729 730 731 732 733 734 735
g_object.off("change", (changeData:ChangeCallback) => {
    console.info("change" + changeData.sessionId);
    if (changeData.fields != null && changeData.fields != undefined) {
        for (let index: number = 0; index < changeData.fields.length; index++) {
            console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]);
        }
    }
});
A
Annie_wang 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
// 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 已提交
754 755 756 757
| 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 已提交
758 759 760 761 762

**Example**

```js
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
interface StatusCallback {
    sessionId: string,
    networkId: string,
    status: 'online' | 'offline'
}
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
A
Annie_wang 已提交
778
}
A
Annie_wang 已提交
779 780 781 782 783
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);

g_object.on("status", (statusCallback:StatusCallback) => {
    console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId);
});
A
Annie_wang 已提交
784 785 786 787 788 789 790 791 792 793
```

### 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 已提交
794
> 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 已提交
795 796 797 798 799

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

**Parameters**

A
Annie_wang 已提交
800 801
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
802
| 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 已提交
803 804 805 806 807 808
| 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 已提交
809
import distributedObject from '@ohos.data.distributedDataObject';
A
Annie_wang 已提交
810 811 812 813 814 815 816 817 818
interface sourceObject{
    name: string,
    age: number,
    isVis: boolean
}
interface offStatusCallback {
    sessionId: string,
    deviceId: string,
    status: 'online' | 'offline'
A
Annie_wang 已提交
819
}
A
Annie_wang 已提交
820 821 822 823 824 825
let source: sourceObject = {
    name: "amy",
    age:18,
    isVis:false
}
let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source);
A
Annie_wang 已提交
826
// Unregister the specified status change callback.
A
Annie_wang 已提交
827 828 829
g_object.off("status", (statusCallback:offStatusCallback) => {
    console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.deviceId);
});
A
Annie_wang 已提交
830 831 832
// Unregister all status change callbacks.
g_object.off("status");
```