1# Lightweight Storage 2 3Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type. 4 5 6> **NOTE** 7> 8> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 9 10 11## Modules to Import 12 13```js 14import data_storage from '@ohos.data.storage'; 15``` 16 17## Constants 18 19**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 20 21| Name | Type| Readable| Writable| Description | 22| ---------------- | -------- | ---- | ---- | ------------------------------------- | 23| MAX_KEY_LENGTH | number | Yes | No | Maximum length of a key. It must be less than 80 bytes. | 24| MAX_VALUE_LENGTH | number | Yes | No | Maximum length of a value. It must be less than 8192 bytes.| 25 26 27## data_storage.getStorageSync 28 29getStorageSync(path: string): Storage 30 31Reads the specified file and loads its data to the **Storage** instance for data operations. 32 33**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 34 35**Parameters** 36 37| Name| Type | Mandatory| Description | 38| ------ | ------ | ---- | -------------------------- | 39| path | string | Yes | Path of the target file.| 40 41**Return value** 42 43| Type | Description | 44| ------------------- | ------------------------------------------------- | 45| [Storage](#storage) | **Storage** instance used for data storage operations.| 46 47**Example** 48 49```js 50import featureAbility from '@ohos.ability.featureAbility'; 51 52var path; 53var context = featureAbility.getContext(); 54context.getFilesDir().then((filePath) => { 55 path = filePath; 56 console.info("======================>getFilesDirPromise====================>"); 57 58 let storage = data_storage.getStorageSync(path + '/mystore'); 59 storage.putSync('startup', 'auto'); 60 storage.flushSync(); 61}); 62``` 63 64 65## data_storage.getStorage 66 67getStorage(path: string, callback: AsyncCallback<Storage>): void 68 69Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses an asynchronous callback to return the result. 70 71**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 72 73**Parameters** 74 75| Name | Type | Mandatory| Description | 76| -------- | ---------------------------------------- | ---- | -------------------------- | 77| path | string | Yes | Path of the target file.| 78| callback | AsyncCallback<[Storage](#storage)> | Yes | Callback invoked to return the result. | 79 80**Example** 81 82```js 83import featureAbility from '@ohos.ability.featureAbility'; 84 85var path; 86var context = featureAbility.getContext(); 87context.getFilesDir().then((filePath) => { 88 path = filePath; 89 console.info("======================>getFilesDirPromise====================>"); 90 91 data_storage.getStorage(path + '/mystore', function (err, storage) { 92 if (err) { 93 console.info("Failed to get the storage. path: " + path + '/mystore'); 94 return; 95 } 96 storage.putSync('startup', 'auto'); 97 storage.flushSync(); 98 }) 99}); 100``` 101 102 103## data_storage.getStorage 104 105getStorage(path: string): Promise<Storage> 106 107Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses a promise to return the result. 108 109**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 110 111**Parameters** 112 113| Name| Type | Mandatory| Description | 114| ------ | ------ | ---- | -------------------------- | 115| path | string | Yes | Path of the target file.| 116 117**Return value** 118 119| Type | Description | 120| ---------------------------------- | ------------------------------- | 121| Promise<[Storage](#storage)> | Promise used to return the result.| 122 123**Example** 124 125```js 126import featureAbility from '@ohos.ability.featureAbility'; 127 128var path; 129var context = featureAbility.getContext(); 130context.getFilesDir().then((filePath) => { 131 path = filePath; 132 console.info("======================>getFilesDirPromise====================>"); 133 134 let getPromise = data_storage.getStorage(path + '/mystore'); 135 getPromise.then((storage) => { 136 storage.putSync('startup', 'auto'); 137 storage.flushSync(); 138 }).catch((err) => { 139 console.info("Failed to get the storage. path: " + path + '/mystore'); 140 }) 141}); 142``` 143 144 145## data_storage.deleteStorageSync 146 147deleteStorageSync(path: string): void 148 149Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. 150 151**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 152 153**Parameters** 154 155| Name| Type | Mandatory| Description | 156| ------ | ------ | ---- | -------------------------- | 157| path | string | Yes | Path of the target file.| 158 159**Example** 160 161```js 162import featureAbility from '@ohos.ability.featureAbility'; 163 164var path; 165var context = featureAbility.getContext(); 166context.getFilesDir().then((filePath) => { 167 path = filePath; 168 console.info("======================>getFilesDirPromise====================>"); 169 170 data_storage.deleteStorageSync(path + '/mystore'); 171}); 172``` 173 174## data_storage.deleteStorage 175 176deleteStorage(path: string, callback: AsyncCallback<void>): void 177 178Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result. 179 180**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 181 182**Parameters** 183 184| Name | Type | Mandatory| Description | 185| -------- | ------------------------- | ---- | -------------------------- | 186| path | string | Yes | Path of the target file.| 187| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 188 189**Example** 190 191```js 192import featureAbility from '@ohos.ability.featureAbility'; 193 194var path; 195var context = featureAbility.getContext(); 196context.getFilesDir().then((filePath) => { 197 path = filePath; 198 console.info("======================>getFilesDirPromise====================>"); 199 200 data_storage.deleteStorage(path + '/mystore', function (err) { 201 if (err) { 202 console.info("Failed to delete the storage with err: " + err); 203 return; 204 } 205 console.info("Succeeded in deleting the storage."); 206 }) 207}); 208``` 209 210 211## data_storage.deleteStorage 212 213deleteStorage(path: string): Promise<void> 214 215Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result. 216 217**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 218 219**Parameters** 220 221| Name| Type | Mandatory| Description | 222| ------ | ------ | ---- | -------------------------- | 223| path | string | Yes | Path of the target file.| 224 225**Return value** 226 227| Type | Description | 228| ------------------- | ------------------------------- | 229| Promise<void> | Promise that returns no value.| 230 231**Example** 232 233```js 234import featureAbility from '@ohos.ability.featureAbility'; 235 236var path; 237var context = featureAbility.getContext(); 238context.getFilesDir().then((filePath) => { 239 path = filePath; 240 console.info("======================>getFilesDirPromise====================>"); 241 242 let promisedelSt = data_storage.deleteStorage(path + '/mystore'); 243 promisedelSt.then(() => { 244 console.info("Succeeded in deleting the storage."); 245 }).catch((err) => { 246 console.info("Failed to delete the storage with err: " + err); 247 }) 248}); 249``` 250 251 252## data_storage.removeStorageFromCacheSync 253 254removeStorageFromCacheSync(path: string): void 255 256Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. 257 258**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 259 260**Parameters** 261| Name| Type | Mandatory| Description | 262| ------ | ------ | ---- | -------------------------- | 263| path | string | Yes | Path of the target file.| 264 265**Example** 266 267```js 268import featureAbility from '@ohos.ability.featureAbility'; 269 270var path; 271var context = featureAbility.getContext(); 272context.getFilesDir().then((filePath) => { 273 path = filePath; 274 console.info("======================>getFilesDirPromise====================>"); 275 276 data_storage.removeStorageFromCacheSync(path + '/mystore'); 277}); 278``` 279 280 281## data_storage.removeStorageFromCache 282 283removeStorageFromCache(path: string, callback: AsyncCallback<void>): void 284 285Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result. 286 287**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 288 289**Parameters** 290 291| Name | Type | Mandatory| Description | 292| -------- | ------------------------- | ---- | -------------------------- | 293| path | string | Yes | Path of the target file.| 294| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 295 296**Example** 297 298```js 299import featureAbility from '@ohos.ability.featureAbility'; 300 301var path; 302var context = featureAbility.getContext(); 303context.getFilesDir().then((filePath) => { 304 path = filePath; 305 console.info("======================>getFilesDirPromise====================>"); 306 307 data_storage.removeStorageFromCache(path + '/mystore', function (err) { 308 if (err) { 309 console.info("Failed to remove storage from cache with err: " + err); 310 return; 311 } 312 console.info("Succeeded in removing storage from cache."); 313 }) 314}); 315``` 316 317 318## data_storage.removeStorageFromCache 319 320removeStorageFromCache(path: string): Promise<void> 321 322Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result. 323 324**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 325 326**Parameters** 327 328| Name| Type | Mandatory| Description | 329| ------ | ------ | ---- | -------------------------- | 330| path | string | Yes | Path of the target file.| 331 332**Return value** 333 334| Type | Description | 335| ------------------- | ------------------------------- | 336| Promise<void> | Promise that returns no value.| 337 338**Example** 339 340```js 341import featureAbility from '@ohos.ability.featureAbility'; 342 343var path; 344var context = featureAbility.getContext(); 345context.getFilesDir().then((filePath) => { 346 path = filePath; 347 console.info("======================>getFilesDirPromise====================>"); 348 349 let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore') 350 promiserevSt.then(() => { 351 console.info("Succeeded in removing storage from cache."); 352 }).catch((err) => { 353 console.info("Failed to remove storage from cache with err: " + err); 354 }) 355}); 356``` 357 358## Storage 359 360Provides APIs for obtaining and modifying storage data. 361 362### getSync 363 364getSync(key: string, defValue: ValueType): ValueType 365 366Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned. 367 368**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 369 370**Parameters** 371 372| Name | Type | Mandatory| Description | 373| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 374| key | string | Yes | Key of the data. It cannot be empty. | 375| defValue | [ValueType](#valuetype) | Yes | Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value.| 376 377**Return value** 378 379| Type | Description | 380| --------- | -------------------------------------------------------- | 381| ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.| 382 383**Example** 384 385```js 386let value = storage.getSync('startup', 'default'); 387console.info("The value of startup is " + value); 388``` 389 390 391### get 392 393get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void 394 395Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned. This API uses an asynchronous callback to return the result. 396 397**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 398 399**Parameters** 400 401| Name | Type | Mandatory| Description | 402| -------- | ------------------------------ | ---- | ----------------------------------------- | 403| key | string | Yes | Key of the data. It cannot be empty. | 404| defValue | [ValueType](#valuetype) | Yes | Default value to be returned. It can be a number, string, or Boolean value.| 405| callback | AsyncCallback<ValueType> | Yes | Callback invoked to return the result. | 406 407**Example** 408 409```js 410storage.get('startup', 'default', function(err, value) { 411 if (err) { 412 console.info("Failed to get the value of startup with err: " + err); 413 return; 414 } 415 console.info("The value of startup is " + value); 416}) 417``` 418 419 420### get 421 422get(key: string, defValue: ValueType): Promise<ValueType> 423 424Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned. This API uses a promise to return the result. 425 426**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 427 428**Parameters** 429 430| Name | Type | Mandatory| Description | 431| -------- | ----------------------- | ---- | ----------------------------------------- | 432| key | string | Yes | Key of the data. It cannot be empty. | 433| defValue | [ValueType](#valuetype) | Yes | Default value to be returned. It can be a number, string, or Boolean value.| 434 435**Return value** 436 437| Type | Description | 438| ------------------------ | ------------------------------- | 439| Promise<ValueType> | Promise used to return the result.| 440 441**Example** 442 443```js 444let promiseget = storage.get('startup', 'default'); 445promiseget.then((value) => { 446 console.info("The value of startup is " + value) 447}).catch((err) => { 448 console.info("Failed to get the value of startup with err: " + err); 449}) 450``` 451 452 453### putSync 454 455putSync(key: string, value: ValueType): void 456 457Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. 458 459**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 460 461**Parameters** 462 463| Name| Type | Mandatory| Description | 464| ------ | ----------------------- | ---- | ----------------------------------------- | 465| key | string | Yes | Key of the data. It cannot be empty. | 466| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value.| 467 468**Example** 469 470```js 471storage.putSync('startup', 'auto'); 472``` 473 474 475### put 476 477put(key: string, value: ValueType, callback: AsyncCallback<void>): void 478 479Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. This API uses an asynchronous callback to return the result. 480 481**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 482 483**Parameters** 484 485| Name | Type | Mandatory| Description | 486| -------- | ------------------------- | ---- | ----------------------------------------- | 487| key | string | Yes | Key of the data. It cannot be empty. | 488| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value.| 489| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 490 491**Example** 492 493```js 494storage.put('startup', 'auto', function (err) { 495 if (err) { 496 console.info("Failed to put the value of startup with err: " + err); 497 return; 498 } 499 console.info("Succeeded in putting the value of startup."); 500}) 501``` 502 503 504### put 505 506put(key: string, value: ValueType): Promise<void> 507 508Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. This API uses a promise to return the result. 509 510**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 511 512**Parameters** 513 514| Name| Type | Mandatory| Description | 515| ------ | ----------------------- | ---- | ----------------------------------------- | 516| key | string | Yes | Key of the data. It cannot be empty. | 517| value | [ValueType](#valuetype) | Yes | New value to store. It can be a number, string, or Boolean value.| 518 519**Return value** 520 521| Type | Description | 522| ------------------- | --------------------------- | 523| Promise<void> | Promise that returns no value.| 524 525**Example** 526 527```js 528let promiseput = storage.put('startup', 'auto'); 529promiseput.then(() => { 530 console.info("Succeeded in putting the value of startup."); 531}).catch((err) => { 532 console.info("Failed to put the value of startup with err: " + err); 533}) 534``` 535 536 537### hasSync 538 539hasSync(key: string): boolean 540 541Checks whether the storage object contains data with a given key. 542 543**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 544 545**Parameters** 546 547| Name| Type | Mandatory| Description | 548| ------ | ------ | ---- | ------------------------------- | 549| key | string | Yes | Key of the data. It cannot be empty.| 550 551**Return value** 552 553| Type | Description | 554| ------- | ------------------------------------- | 555| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.| 556 557**Example** 558 559```js 560let isExist = storage.hasSync('startup'); 561if (isExist) { 562 console.info("The key of startup is contained."); 563} 564``` 565 566 567### has 568 569has(key: string, callback: AsyncCallback<boolean>): boolean 570 571Checks whether the storage object contains data with a given key. This API uses an asynchronous callback to return the result. 572 573**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 574 575**Parameters** 576 577| Name | Type | Mandatory| Description | 578| -------- | ---------------------------- | ---- | ------------------------------- | 579| key | string | Yes | Key of the data. It cannot be empty.| 580| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. | 581 582**Return value** 583 584| Type | Description | 585| ------- | ------------------------------- | 586| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.| 587 588**Example** 589 590```js 591storage.has('startup', function (err, isExist) { 592 if (err) { 593 console.info("Failed to check the key of startup with err: " + err); 594 return; 595 } 596 if (isExist) { 597 console.info("The key of startup is contained."); 598 } 599}) 600``` 601 602 603### has 604 605has(key: string): Promise<boolean> 606 607Checks whether the storage object contains data with a given key. This API uses a promise to return the result. 608 609**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 610 611**Parameters** 612 613| Name| Type | Mandatory| Description | 614| ------ | ------ | ---- | ------------------------------- | 615| key | string | Yes | Key of the data. It cannot be empty.| 616 617**Return value** 618 619| Type | Description | 620| ---------------------- | --------------------------- | 621| Promise<boolean> | Promise used to return the result.| 622 623**Example** 624 625```js 626let promisehas = storage.has('startup') 627promisehas.then((isExist) => { 628 if (isExist) { 629 console.info("The key of startup is contained."); 630 } 631}).catch((err) => { 632 console.info("Failed to check the key of startup with err: " + err); 633}) 634``` 635 636 637### deleteSync 638 639deleteSync(key: string): void 640 641Deletes data with the specified key from this storage object. 642 643**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 644 645**Parameters** 646 647| Name| Type | Mandatory| Description | 648| ------ | ------ | ---- | --------------------------------- | 649| key | string | Yes | Key of the data. It cannot be empty.| 650 651**Example** 652 653```js 654 storage.deleteSync('startup'); 655``` 656 657 658### delete 659 660delete(key: string, callback: AsyncCallback<void>): void 661 662Deletes data with the specified key from this storage object. This API uses an asynchronous callback to return the result. 663 664**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 665 666**Parameters** 667 668| Name | Type | Mandatory| Description | 669| -------- | ------------------------- | ---- | ------------------------------- | 670| key | string | Yes | Key of the data. It cannot be empty.| 671| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 672 673**Example** 674 675```js 676storage.delete('startup', function (err) { 677 if (err) { 678 console.info("Failed to delete startup key failed err: " + err); 679 return; 680 } 681 console.info("Succeeded in deleting startup key."); 682}) 683``` 684 685 686### delete 687 688delete(key: string): Promise<void> 689 690Deletes data with the specified key from this storage object. This API uses a promise to return the result. 691 692**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 693 694**Parameters** 695 696| Name| Type | Mandatory| Description | 697| ------ | ------ | ---- | --------------------- | 698| key | string | Yes | Key of the data.| 699 700**Return value** 701 702| Type | Description | 703| ------------------- | --------------------------- | 704| Promise<void> | Promise that returns no value.| 705 706**Example** 707 708```js 709let promisedel = storage.delete('startup') 710promisedel.then(() => { 711 console.info("Succeeded in deleting startup key."); 712}).catch((err) => { 713 console.info("Failed to delete startup key failed err: " + err); 714}) 715``` 716 717 718### flushSync 719 720flushSync(): void 721 722Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. 723 724**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 725 726**Example** 727 728```js 729storage.flushSync(); 730``` 731 732 733### flush 734 735flush(callback: AsyncCallback<void>): void 736 737Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses an asynchronous callback to return the result. 738 739**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 740 741**Parameters** 742 743| Name | Type | Mandatory| Description | 744| -------- | ------------------------- | ---- | ---------- | 745| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 746 747**Example** 748 749```js 750storage.flush(function (err) { 751 if (err) { 752 console.info("Failed to flush to file with err: " + err); 753 return; 754 } 755 console.info("Succeeded in flushing to file."); 756}) 757``` 758 759 760### flush 761 762flush(): Promise<void> 763 764Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses a promise to return the result. 765 766**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 767 768**Return value** 769 770| Type | Description | 771| ------------------- | --------------------------- | 772| Promise<void> | Promise that returns no value.| 773 774**Example** 775 776```js 777let promiseflush = storage.flush(); 778promiseflush.then(() => { 779 console.info("Succeeded in flushing to file."); 780}).catch((err) => { 781 console.info("Failed to flush to file with err: " + err); 782}) 783``` 784 785 786### clearSync 787 788clearSync(): void 789 790Clears this **Storage** object. 791 792**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 793 794**Example** 795 796```js 797storage.clearSync(); 798``` 799 800 801### clear 802 803clear(callback: AsyncCallback<void>): void 804 805Clears this **Storage** object. This API uses an asynchronous callback to return the result. 806 807**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 808 809**Parameters** 810 811| Name | Type | Mandatory| Description | 812| -------- | ------------------------- | ---- | ---------- | 813| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 814 815**Example** 816 817```js 818storage.clear(function (err) { 819 if (err) { 820 console.info("Failed to clear the storage with err: " + err); 821 return; 822 } 823 console.info("Succeeded in clearing the storage."); 824}) 825``` 826 827 828### clear 829 830clear(): Promise<void> 831 832Clears this **Storage** object. This API uses a promise to return the result. 833 834**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 835 836**Return value** 837| Type | Description | 838| ------------------- | --------------------------- | 839| Promise<void> | Promise that returns no value.| 840 841**Example** 842 843```js 844let promiseclear = storage.clear(); 845promiseclear.then(() => { 846 console.info("Succeeded in clearing the storage."); 847}).catch((err) => { 848 console.info("Failed to clear the storage with err: " + err); 849}) 850``` 851 852 853### on('change') 854 855on(type: 'change', callback: Callback<StorageObserver>): void 856 857Subscribes to data changes. The **StorageObserver** needs to be implemented. When the value of the key subscribed to is changed, a callback will be invoked after **flush()** or **flushSync()** is executed. 858 859**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 860 861**Parameters** 862 863| Name | Type | Description | 864| -------- | --------------------------------------------------- | ---------------------------------------- | 865| type | string | Event type. The value **change** indicates data change events.| 866| callback | Callback<[StorageObserver](#storageobserver)> | Callback invoked to return the data change. | 867 868**Example** 869 870```js 871var observer = function (key) { 872 console.info("The key of " + key + " changed."); 873} 874storage.on('change', observer); 875storage.putSync('startup', 'auto'); 876storage.flushSync(); // observer will be called. 877``` 878 879 880### off('change') 881 882off(type: 'change', callback: Callback<StorageObserver>): void 883 884Unsubscribes from data changes. 885 886**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 887 888**Parameters** 889 890| Name | Type | Description | 891| -------- | --------------------------------------------------- | ---------------------------------------- | 892| type | string | Event type. The value **change** indicates data change events.| 893| callback | Callback<[StorageObserver](#storageobserver)> | Callback for the data change. | 894 895**Example** 896 897```js 898var observer = function (key) { 899 console.info("The key of " + key + " changed."); 900} 901storage.off('change', observer); 902``` 903 904 905## StorageObserver 906 907**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 908 909| Name| Type| Mandatory| Description | 910| ---- | -------- | ---- | ---------------- | 911| key | string | No | Data changed.| 912 913## ValueType 914 915Enumerates the value types. 916 917**System capability**: SystemCapability.DistributedDataManager.Preferences.Core 918 919| Type | Description | 920| ------- | -------------------- | 921| number | The value is a number. | 922| string | The value is a string. | 923| boolean | The value is of Boolean type.| 924