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