1# @ohos.data.distributedDataObject (Distributed Data Object) 2 3The **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. 4 5> **NOTE** 6> 7> 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. 8> 9 10 11## Modules to Import 12 13```ts 14import distributedObject from '@ohos.data.distributedDataObject'; 15``` 16 17## distributedObject.create<sup>9+</sup> 18 19create(context: Context, source: object): DataObject 20 21Creates a distributed data object. 22 23**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 24 25**Parameters** 26 27| Name| Type| Mandatory| Description| 28| -------- | -------- | -------- | -------- | 29| 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).| 30| source | object | Yes| Attributes of the distributed data object.| 31 32**Return value** 33 34| Type| Description| 35| -------- | -------- | 36| [DataObject](#dataobject) | Distributed data object created.| 37 38**Example** 39 40FA model: 41 42```ts 43// Import the module. 44import distributedObject from '@ohos.data.distributedDataObject'; 45import featureAbility from '@ohos.ability.featureAbility'; 46import { BusinessError } from '@ohos.base'; 47// Obtain the context. 48let context = featureAbility.getContext(); 49class SourceObject { 50 name: string 51 age: number 52 isVis: boolean 53 54 constructor(name: string, age: number, isVis: boolean) { 55 this.name = name 56 this.age = age 57 this.isVis = isVis 58 } 59} 60 61let source: SourceObject = new SourceObject("amy", 18, false); 62let g_object: distributedObject.DataObject = distributedObject.create(context, source); 63``` 64 65Stage model: 66 67```ts 68// Import the module. 69import distributedObject from '@ohos.data.distributedDataObject'; 70import UIAbility from '@ohos.app.ability.UIAbility'; 71import { BusinessError } from '@ohos.base'; 72import window from '@ohos.window'; 73 74let g_object: distributedObject.DataObject|null = null; 75class SourceObject { 76 name: string 77 age: number 78 isVis: boolean 79 80 constructor(name: string, age: number, isVis: boolean) { 81 this.name = name 82 this.age = age 83 this.isVis = isVis 84 } 85} 86 87class EntryAbility extends UIAbility { 88 onWindowStageCreate(windowStage: window.WindowStage) { 89 let source: SourceObject = new SourceObject("amy", 18, false); 90 let g_object: distributedObject.DataObject = distributedObject.create(this.context, source); 91 } 92} 93``` 94 95## distributedObject.genSessionId 96 97genSessionId(): string 98 99Creates a random session ID. 100 101**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 102 103**Return value** 104 105| Type| Description| 106| -------- | -------- | 107| string | Session ID created.| 108 109**Example** 110 111```ts 112import distributedObject from '@ohos.data.distributedDataObject'; 113let sessionId: string = distributedObject.genSessionId(); 114``` 115 116## SaveSuccessResponse<sup>9+</sup> 117 118Called when the **Save()** API is successfully called. 119 120**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 121 122| Name| Type| Mandatory| Description| 123| -------- | -------- | -------- | -------- | 124| sessionId | string | Yes| Unique ID for multi-device collaboration.| 125| version | number | Yes| Version of the distributed data object saved.| 126| 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.| 127 128## RevokeSaveSuccessResponse<sup>9+</sup> 129 130Called when the **revokeSave()** API is successfully called. 131 132**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 133 134| Name| Type| Mandatory| Description| 135| -------- | -------- | -------- | -------- | 136| sessionId | string | Yes| Unique ID for multi-device collaboration.| 137 138## DataObject 139 140Provides APIs for managing a distributed data object. Before using any API of this class, use [create()](#distributedobjectcreate9) to create a **DataObject** object. 141 142### setSessionId<sup>9+</sup> 143 144setSessionId(sessionId: string, callback: AsyncCallback<void>): void 145 146Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network. 147 148**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 149 150**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 151 152**Parameters** 153 154| Name| Type| Mandatory| Description| 155| -------- | -------- | -------- | -------- | 156| sessionId | string | Yes| ID of a distributed data object on a trusted network.| 157| callback | AsyncCallback<void> | Yes| Asynchronous callback invoked when the session ID is successfully set.| 158 159**Error codes** 160 161 For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md). 162 163| ID| Error Message| 164| -------- | -------- | 165| 15400001 | Create table failed.| 166 167**Example** 168 169```ts 170// Add g_object to the distributed network. 171g_object.setSessionId(distributedObject.genSessionId(), ()=>{ 172 console.info("join session"); 173}); 174``` 175 176### setSessionId<sup>9+</sup> 177 178setSessionId(callback: AsyncCallback<void>): void 179 180Exits all joined sessions. 181 182**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 183 184**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 185 186**Parameters** 187 188| Name| Type| Mandatory| Description| 189| -------- | -------- | -------- | -------- | 190| callback | AsyncCallback<void> | Yes| Asynchronous callback invoked when the distributed data object exits all joined sessions.| 191 192**Error codes** 193 194 For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md). 195 196| ID| Error Message| 197| -------- | -------- | 198| 15400001 | Create table failed.| 199 200**Example** 201 202```ts 203// Add g_object to the distributed network. 204g_object.setSessionId(distributedObject.genSessionId(), ()=>{ 205 console.info("join session"); 206}); 207// Exit the distributed network. 208g_object.setSessionId(() => { 209 console.info("leave all session."); 210}); 211``` 212 213### setSessionId<sup>9+</sup> 214 215setSessionId(sessionId?: string): Promise<void> 216 217Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network. 218 219**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 220 221**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 222 223**Parameters** 224 225| Name| Type| Mandatory| Description| 226| -------- | -------- | -------- | -------- | 227| 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.| 228 229**Return value** 230 231| Type| Description| 232| -------- | -------- | 233| Promise<void> | Promise that returns no value.| 234 235**Error codes** 236 237 For details about the error codes, see [Distributed Data Object Error Codes](../errorcodes/errorcode-distributed-dataObject.md). 238 239| ID| Error Message| 240| -------- | -------- | 241| 15400001 | Create table failed.| 242 243**Example** 244 245```ts 246// Add g_object to the distributed network. 247g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ 248 console.info("join session."); 249 }).catch((error: BusinessError)=>{ 250 console.info("error:" + error.code + error.message); 251}); 252// Exit the distributed network. 253g_object.setSessionId().then (()=>{ 254 console.info("leave all session."); 255 }).catch((error: BusinessError)=>{ 256 console.info("error:" + error.code + error.message); 257}); 258``` 259 260### on('change')<sup>9+</sup> 261 262on(type: 'change', callback: (sessionId: string, fields: Array<string>) => void ): void 263 264Subscribes to data changes of this distributed data object. 265 266**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 267 268**Parameters** 269 270| Name| Type| Mandatory| Description| 271| -------- | -------- | -------- | -------- | 272| type | string | Yes| Event type. The value is **change**, which indicates data changes.| 273| callback | Function | 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.| 274 275**Example** 276 277```ts 278g_object.on("change", (sessionId: string, fields: Array<string>) => { 279 console.info("change" + sessionId); 280 if (fields != null && fields != undefined) { 281 for (let index: number = 0; index < fields.length; index++) { 282 console.info("changed !" + fields[index] + " " + g_object[fields[index]]); 283 } 284 } 285}); 286``` 287 288### off('change')<sup>9+</sup> 289 290off(type: 'change', callback?: (sessionId: string, fields: Array<string>) => void ): void 291 292Unsubscribes from the data changes of this distributed data object. 293 294**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 295 296**Parameters** 297 298| Name| Type| Mandatory| Description| 299| -------- | -------- | -------- | -------- | 300| type | string | Yes| Event type. The value is **change**, which indicates data changes.| 301| callback | Function | 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.| 302 303 304**Example** 305 306```ts 307// Unregister the specified data change callback. 308g_object.off("change", (sessionId: string, fields: Array<string>) => { 309 console.info("change" + sessionId); 310 if (fields != null && fields != undefined) { 311 for (let index: number = 0; index < fields.length; index++) { 312 console.info("changed !" + fields[index] + " " + g_object[fields[index]]); 313 } 314 } 315}); 316// Unregister all data change callbacks. 317g_object.off("change"); 318``` 319 320### on('status')<sup>9+</sup> 321 322on(type: 'status', callback: (sessionId: string, networkId: string, status: 'online' \| 'offline' ) => void): void 323 324Subscribes to status changes of this distributed data object. 325 326**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 327 328**Parameters** 329 330| Name| Type| Mandatory| Description| 331| -------- | -------- | -------- | -------- | 332| type | string | Yes| Event type. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| 333| callback | Function | Yes| Callback invoked to return the status change.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** identifies the device.<br>**status** indicates the object status, which can be online or offline.| 334 335**Example** 336 337```ts 338g_object.on("status", (sessionId: string, networkId: string, status: 'online' | 'offline') => { 339 console.info("status changed " + sessionId + " " + status + " " + networkId); 340}); 341``` 342 343### off('status')<sup>9+</sup> 344 345off(type: 'status', callback?:(sessionId: string, networkId: string, status: 'online' \| 'offline') => void): void 346 347Unsubscribes from the status change of this distributed data object. 348 349**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 350 351**Parameters** 352 353| Name| Type| Mandatory| Description| 354| -------- | -------- | -------- | -------- | 355| type | string | Yes| Event type. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| 356| callback | Function | 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>**networkId** identifies the distributed data object.<br>**status** indicates the object status, which can be online or offline.| 357 358 359**Example** 360 361```ts 362// Unregister the specified status change callback. 363g_object.off("status", (sessionId: string, networkId: string, status: 'online' | 'offline') => { 364 console.info("status changed " + sessionId + " " + status + " " + networkId); 365}); 366// Unregister all status change callbacks. 367g_object.off("status"); 368``` 369 370### save<sup>9+</sup> 371 372save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void 373 374Saves a distributed data object. This API uses an asynchronous callback to return the result. 375 376If 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. 377 378The saved data will be released in the following cases: 379 380- The data is stored for more than 24 hours. 381- The application has been uninstalled. 382- Data is successfully restored. 383 384**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 385 386**Parameters** 387 388| Name| Type| Mandatory| Description| 389| -------- | -------- | -------- | -------- | 390| deviceId | string | Yes| ID of the device where data is stored. The value **local** indicates the local device.| 391| callback | AsyncCallback<[SaveSuccessResponse](#savesuccessresponse9)> | Yes| Callback invoked to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.| 392 393**Example** 394 395```ts 396g_object.setSessionId("123456"); 397g_object.save("local", (err: BusinessError, result:distributedObject.SaveSuccessResponse) => { 398 if (err) { 399 console.info("save failed, error code = " + err.code); 400 console.info("save failed, error message: " + err.message); 401 return; 402 } 403 console.info("save callback"); 404 console.info("save sessionId: " + result.sessionId); 405 console.info("save version: " + result.version); 406 console.info("save deviceId: " + result.deviceId); 407}); 408``` 409 410### save<sup>9+</sup> 411 412save(deviceId: string): Promise<SaveSuccessResponse> 413 414Saves a distributed data object. This API uses a promise to return the result. 415 416If 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. 417 418The saved data will be released in the following cases: 419 420- The data is stored for more than 24 hours. 421- The application has been uninstalled. 422- Data is successfully restored. 423 424**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 425 426**Parameters** 427 428| Name| Type| Mandatory| Description| 429| -------- | -------- | -------- | -------- | 430| deviceId | string | Yes| ID of the device where the data is saved. The default value is **local**, which indicates the local device. | 431 432**Return value** 433 434| Type| Description| 435| -------- | -------- | 436| Promise<[SaveSuccessResponse](#savesuccessresponse9)> | Promise used to return **SaveSuccessResponse**, which contains information such as session ID, version, and device ID.| 437 438**Example** 439 440```ts 441g_object.setSessionId("123456"); 442g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => { 443 console.info("save callback"); 444 console.info("save sessionId " + result.sessionId); 445 console.info("save version " + result.version); 446 console.info("save deviceId " + result.deviceId); 447}).catch((err: BusinessError) => { 448 console.info("save failed, error code = " + err.code); 449 console.info("save failed, error message: " + err.message); 450}); 451``` 452 453### revokeSave<sup>9+</sup> 454 455revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void 456 457Revokes the data of this distributed data object saved. This API uses an asynchronous callback to return the result. 458 459If the object is saved on the local device, the data saved on all trusted devices will be deleted. 460If the object is stored on another device, the data on the local device will be deleted. 461 462**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 463 464**Parameters** 465 466| Name| Type| Mandatory| Description| 467| -------- | -------- | -------- | -------- | 468| callback | AsyncCallback<[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)> | Yes| Callback invoked to return **RevokeSaveSuccessResponse**, which contains the session ID.| 469 470**Example** 471 472```ts 473g_object.setSessionId("123456"); 474// Save data for persistence. 475g_object.save("local", (err: BusinessError, result: distributedObject.SaveSuccessResponse) => { 476 if (err) { 477 console.info("save failed, error code = " + err.code); 478 console.info("save failed, error message: " + err.message); 479 return; 480 } 481 console.info("save callback"); 482 console.info("save sessionId: " + result.sessionId); 483 console.info("save version: " + result.version); 484 console.info("save deviceId: " + result.deviceId); 485}); 486// Delete the persistence data. 487g_object.revokeSave((err: BusinessError, result: distributedObject.RevokeSaveSuccessResponse) => { 488 if (err) { 489 console.info("revokeSave failed, error code = " + err.code); 490 console.info("revokeSave failed, error message: " + err.message); 491 return; 492 } 493 console.info("revokeSave callback"); 494 console.info("revokeSave sessionId " + result.sessionId); 495}); 496``` 497 498### revokeSave<sup>9+</sup> 499 500revokeSave(): Promise<RevokeSaveSuccessResponse> 501 502Revokes the data of this distributed data object saved. This API uses a promise to return the result. 503 504If the object is saved on the local device, the data saved on all trusted devices will be deleted. 505If the object is stored on another device, the data on the local device will be deleted. 506 507**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 508 509**Return value** 510 511| Type| Description| 512| -------- | -------- | 513| Promise<[RevokeSaveSuccessResponse](#revokesavesuccessresponse9)> | Promise used to return **RevokeSaveSuccessResponse**, which contains the session ID.| 514 515**Example** 516 517```ts 518g_object.setSessionId("123456"); 519// Save data for persistence. 520g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => { 521 console.info("save callback"); 522 console.info("save sessionId " + result.sessionId); 523 console.info("save version " + result.version); 524 console.info("save deviceId " + result.deviceId); 525}).catch((err: BusinessError) => { 526 console.info("save failed, error code = " + err.code); 527 console.info("save failed, error message: " + err.message); 528}); 529// Delete the persistence data. 530g_object.revokeSave().then((result: distributedObject.RevokeSaveSuccessResponse) => { 531 console.info("revokeSave callback"); 532 console.info("sessionId" + result.sessionId); 533}).catch((err: BusinessError)=> { 534 console.info("revokeSave failed, error code = " + err.code); 535 console.info("revokeSave failed, error message = " + err.message); 536}); 537``` 538 539## distributedObject.createDistributedObject<sup>(deprecated)</sup> 540 541createDistributedObject(source: object): DistributedObject 542 543 544Creates a distributed data object. 545 546> **NOTE**<br/> 547> 548> This API is supported since API version 8 and deprecated since API version 9. You are advised to use **distributedObject.create**. 549 550**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 551 552**Parameters** 553 554| Name| Type| Mandatory| Description| 555| -------- | -------- | -------- | -------- | 556| source | object | Yes| Attributes of the distributed data object.| 557 558**Return value** 559 560| Type| Description| 561| -------- | -------- | 562| [DistributedObject](#distributedobjectdeprecated) | Distributed data object created.| 563 564**Example** 565 566```ts 567import distributedObject from '@ohos.data.distributedDataObject'; 568class SourceObject { 569 name: string 570 age: number 571 isVis: boolean 572 573 constructor(name: string, age: number, isVis: boolean) { 574 this.name = name 575 this.age = age 576 this.isVis = isVis 577 } 578} 579 580let source: SourceObject = new SourceObject("amy", 18, false); 581let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 582``` 583 584## DistributedObject<sup>(deprecated)</sup> 585 586Provides APIs for managing a distributed data object. Before using any API of this class, use [createDistributedObject()](#distributedobjectcreatedistributedobjectdeprecated) to create a **DistributedObject** object. 587 588### setSessionId<sup>(deprecated)</sup> 589 590setSessionId(sessionId?: string): boolean 591 592Sets a session ID for synchronization. Automatic synchronization is performed for multiple devices with the same session ID on a trusted network. 593 594> **NOTE** 595> 596> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [setSessionId](#setsessionid9). 597 598**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 599 600**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 601 602**Parameters** 603 604| Name| Type| Mandatory| Description| 605| -------- | -------- | -------- | -------- | 606| 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.| 607 608**Return value** 609 610| Type| Description| 611| -------- | -------- | 612| boolean | Returns **true** if the session ID is set successfully;<br>returns **false** otherwise. | 613 614**Example** 615 616```ts 617import distributedObject from '@ohos.data.distributedDataObject'; 618class SourceObject { 619 name: string 620 age: number 621 isVis: boolean 622 623 constructor(name: string, age: number, isVis: boolean) { 624 this.name = name 625 this.age = age 626 this.isVis = isVis 627 } 628} 629 630let source: SourceObject = new SourceObject("amy", 18, false); 631let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 632// Add g_object to the distributed network. 633g_object.setSessionId(distributedObject.genSessionId()); 634// Remove g_object from the distributed network. 635g_object.setSessionId(""); 636``` 637 638### on('change')<sup>(deprecated)</sup> 639 640on(type: 'change', callback: (sessionId: string, fields: Array<string>) => void): void 641 642Subscribes to data changes of this distributed data object. 643 644> **NOTE** 645> 646> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [on('change')](#onchange9). 647 648**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 649 650**Parameters** 651 652| Name| Type| Mandatory| Description| 653| -------- | -------- | -------- | -------- | 654| type | string | Yes| Event type. The value is **change**, which indicates data changes.| 655| callback | Function | 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.| 656 657**Example** 658 659```ts 660import distributedObject from '@ohos.data.distributedDataObject'; 661class SourceObject { 662 name: string 663 age: number 664 isVis: boolean 665 666 constructor(name: string, age: number, isVis: boolean) { 667 this.name = name 668 this.age = age 669 this.isVis = isVis 670 } 671} 672 673let source: SourceObject = new SourceObject("amy", 18, false); 674let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 675g_object.on("change", (sessionId: string, fields: Array<string>) => { 676 console.info("change" + sessionId); 677 if (fields != null && fields != undefined) { 678 for (let index: number = 0; index < fields.length; index++) { 679 console.info("changed !" + fields[index] + " " + g_object[fields[index]]); 680 } 681 } 682}); 683``` 684 685### off('change')<sup>(deprecated)</sup> 686 687off(type: 'change', callback:? (sessionId: string, fields: Array<string>) => void): void 688 689Unsubscribes from the data changes of this distributed data object. 690 691> **NOTE** 692> 693> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [off('change')](#offchange9). 694 695**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 696 697**Parameters** 698 699| Name| Type| Mandatory| Description| 700| -------- | -------- | -------- | -------- | 701| type | string | Yes| Event type. The value is **change**, which indicates data changes.| 702| callback | Function | 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.| 703 704**Example** 705 706```ts 707import distributedObject from '@ohos.data.distributedDataObject'; 708class SourceObject { 709 name: string 710 age: number 711 isVis: boolean 712 713 constructor(name: string, age: number, isVis: boolean) { 714 this.name = name 715 this.age = age 716 this.isVis = isVis 717 } 718} 719 720let source: SourceObject = new SourceObject("amy", 18, false); 721let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 722// Unregister the specified data change callback. 723g_object.off("change", (sessionId: string, fields: Array<string>) => { 724 console.info("change" + sessionId); 725 if (fields != null && fields != undefined) { 726 for (let index: number = 0; index < fields.length; index++) { 727 console.info("changed !" + fields[index] + " " + g_object[fields[index]]); 728 } 729 } 730}); 731// Unregister all data change callbacks. 732g_object.off("change"); 733``` 734 735### on('status')<sup>(deprecated)</sup> 736 737on(type: 'status', callback: (sessionId: string, networkId: string, status: 'online' | 'offline' ) => void): void 738 739Subscribes to status changes of this distributed data object. 740 741> **NOTE** 742> 743> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [on('status')](#onstatus9). 744 745**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 746 747**Parameters** 748 749| Name| Type| Mandatory| Description| 750| -------- | -------- | -------- | -------- | 751| type | string | Yes| Event type. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| 752| callback | Function | Yes| Callback invoked to return the status change.<br>**sessionId** indicates the session ID of the distributed data object.<br>**networkId** identifies the device.<br>**status** indicates the object status, which can be online or offline.| 753 754**Example** 755 756```ts 757import distributedObject from '@ohos.data.distributedDataObject'; 758class SourceObject { 759 name: string 760 age: number 761 isVis: boolean 762 763 constructor(name: string, age: number, isVis: boolean) { 764 this.name = name 765 this.age = age 766 this.isVis = isVis 767 } 768} 769 770let source: SourceObject = new SourceObject("amy", 18, false); 771let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 772 773g_object.on("status", (sessionId: string, networkId: string, status: 'online' | 'offline') => { 774 console.info("status changed " + sessionId + " " + status + " " + networkId); 775}); 776``` 777 778### off('status')<sup>(deprecated)</sup> 779 780off(type: 'status', callback?: (sessionId: string, networkId: string, status: 'online' | 'offline' ) => void): void 781 782Unsubscribes from the status change of this distributed data object. 783 784> **NOTE** 785> 786> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [off('status')](#offstatus9). 787 788**System capability**: SystemCapability.DistributedDataManager.DataObject.DistributedObject 789 790**Parameters** 791 792| Name| Type| Mandatory| Description| 793| -------- | -------- | -------- | -------- | 794| type | string | Yes| Event type. The value is **status**, which indicates the status change (online or offline) of the distributed data object.| 795| callback | Function | 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>**networkId** identifies the distributed data object.<br>**status** indicates the object status, which can be online or offline.| 796 797 798**Example** 799 800```ts 801import distributedObject from '@ohos.data.distributedDataObject'; 802class SourceObject { 803 name: string 804 age: number 805 isVis: boolean 806 807 constructor(name: string, age: number, isVis: boolean) { 808 this.name = name 809 this.age = age 810 this.isVis = isVis 811 } 812} 813 814let source: SourceObject = new SourceObject("amy", 18, false); 815let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); 816// Unregister the specified status change callback. 817g_object.off("status", (sessionId: string, networkId: string, status: 'online' | 'offline') => { 818 console.info("status changed " + sessionId + " " + status + " " + networkId); 819}); 820// Unregister all status change callbacks. 821g_object.off("status"); 822``` 823