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