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