js-apis-data-storage.md 25.3 KB
Newer Older
A
annie_wangli 已提交
1
# Lightweight Storage
Z
zengyawen 已提交
2

A
annie_wangli 已提交
3
Lightweight 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.
Z
zengyawen 已提交
4 5


A
annie_wangli 已提交
6
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
A
annie_wangli 已提交
7 8 9 10
>
> - 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.
>
> - 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).
A
annie_wangli 已提交
11 12 13


## Modules to Import
Z
zengyawen 已提交
14

A
Annie_wang 已提交
15 16
```js
import dataStorage from '@ohos.data.storage';
Z
zengyawen 已提交
17 18
```

A
Annie_wang 已提交
19
## Constants
A
annie_wangli 已提交
20 21 22 23 24 25

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It is 80 bytes.|
A
Annie_wang 已提交
26
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It is 8192 bytes.|
A
annie_wangli 已提交
27 28 29 30 31 32 33 34 35 36


## dataStorage.getStorageSync

getStorageSync(path: string): Storage

Reads a file and loads the data to the **Storage** instance in synchronous mode.

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
37
**Parameters**
A
annie_wangli 已提交
38 39 40 41
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
42
**Return value**
A
annie_wangli 已提交
43 44 45 46
  | Type| Description|
  | -------- | -------- |
  | [Storage](#storage) | **Storage** instance used for data storage operations.|

A
Annie_wang 已提交
47 48
**Example**
  ```js
A
annie_wangli 已提交
49 50
  import dataStorage from '@ohos.data.storage'
  import featureAbility from '@ohos.ability.featureAbility'
A
annie_wangli 已提交
51
  
A
annie_wangli 已提交
52
  var context = featureAbility.getContext()
A
annie_wangli 已提交
53 54 55 56 57 58 59 60 61 62
  context.getFilesDir((err, path) => {
      if (err) {
          console.error('getFilesDir failed. err: ' + JSON.stringify(err));
          return;
      }
      console.info('getFilesDir successful. path:' + JSON.stringify(path));
      let storage = dataStorage.getStorageSync(path + '/mystore')
      storage.putSync('startup', 'auto')
      storage.flushSync()
  });
A
annie_wangli 已提交
63 64 65 66 67 68 69
  ```


## dataStorage.getStorage

getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void

A
Annie_wang 已提交
70
Reads a file and loads the data to the **Storage** instance. This API uses an asynchronous callback to return the execution result.
A
annie_wangli 已提交
71 72 73

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
74
**Parameters**
A
annie_wangli 已提交
75 76 77 78 79
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
80 81
**Example**
  ```js
A
annie_wangli 已提交
82 83
  import dataStorage from '@ohos.data.storage'
  import featureAbility from '@ohos.ability.featureAbility'
A
annie_wangli 已提交
84
  
A
annie_wangli 已提交
85
  var context = featureAbility.getContext()
A
annie_wangli 已提交
86
  context.getFilesDir((err, path) => {
A
annie_wangli 已提交
87
      if (err) {
A
annie_wangli 已提交
88
          console.error('getFilesDir failed. err: ' + JSON.stringify(err));
A
annie_wangli 已提交
89 90
          return;
      }
A
annie_wangli 已提交
91 92 93 94 95 96 97 98 99 100
      console.info('getFilesDir successful. path:' + JSON.stringify(path));
      dataStorage.getStorage(path + '/mystore', function (err, storage) {
          if (err) {
              console.info("Get the storage failed, path: " + path + '/mystore')
              return;
          }
          storage.putSync('startup', 'auto')
          storage.flushSync()
      })
  });
A
annie_wangli 已提交
101
  ```
Z
zengyawen 已提交
102 103


A
annie_wangli 已提交
104 105 106 107
## dataStorage.getStorage

getStorage(path: string): Promise&lt;Storage&gt;

A
Annie_wang 已提交
108
Reads a file and loads the data to the **Storage** instance. This API uses a promise to return the execution result.
A
annie_wangli 已提交
109 110 111

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
112
**Parameters**
A
annie_wangli 已提交
113 114 115 116
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
117
**Return value**
A
annie_wangli 已提交
118 119 120 121
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
122 123
**Example**
  ```js
A
annie_wangli 已提交
124 125
  import dataStorage from '@ohos.data.storage'
  import featureAbility from '@ohos.ability.featureAbility'
A
annie_wangli 已提交
126
  
A
annie_wangli 已提交
127
  var context = featureAbility.getContext()
A
annie_wangli 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
  context.getFilesDir((err, path) => {
      if (err) {
          console.info("Get the storage failed, path: " + path + '/mystore')
          return;
      }
      console.info('getFilesDir successful. path:' + JSON.stringify(path));
      let promisegetSt = dataStorage.getStorage(path + '/mystore')
      promisegetSt.then((storage) => {
          storage.putSync('startup', 'auto')
          storage.flushSync()
      }).catch((err) => {
          console.info("Get the storage failed, path: " + path + '/mystore')
      })
  });
A
annie_wangli 已提交
142
  ```
Z
zengyawen 已提交
143 144


A
annie_wangli 已提交
145 146 147 148
## dataStorage.deleteStorageSync

deleteStorageSync(path: string): void

A
Annie_wang 已提交
149
Deletes 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 synchronous mode.
A
annie_wangli 已提交
150 151 152

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
153
**Parameters**
A
annie_wangli 已提交
154 155 156 157
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
158 159
**Example**
  ```js
A
annie_wangli 已提交
160 161 162 163 164 165 166 167
  dataStorage.deleteStorageSync(path + '/mystore')
  ```


## dataStorage.deleteStorage

deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
168
Deletes 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 execution result.
A
annie_wangli 已提交
169 170 171

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
172
**Parameters**
A
annie_wangli 已提交
173 174 175 176 177
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
178 179
**Example**
  ```js
A
annie_wangli 已提交
180 181 182 183 184 185 186 187 188
  dataStorage.deleteStorage(path + '/mystore', function (err) {
      if (err) {
          console.info("Deleted failed with err: " + err)
          return
      }
      console.info("Deleted successfully.")
  })
  ```

Z
zengyawen 已提交
189

A
annie_wangli 已提交
190
## dataStorage.deleteStorage
Z
zengyawen 已提交
191

A
annie_wangli 已提交
192 193
deleteStorage(path: string): Promise&lt;void&gt;

A
Annie_wang 已提交
194
Deletes 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 execution result.
A
annie_wangli 已提交
195 196 197

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
198
**Parameters**
A
annie_wangli 已提交
199 200 201 202
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
203
**Return value**
A
annie_wangli 已提交
204 205 206 207
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
208 209
**Example**
  ```js
A
annie_wangli 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
  let promisedelSt = dataStorage.deleteStorage(path + '/mystore')
  promisedelSt.then(() => {
      console.info("Deleted successfully.")
  }).catch((err) => {
      console.info("Deleted failed with err: " + err)
  })
  ```


## dataStorage.removeStorageFromCacheSync

removeStorageFromCacheSync(path: string): void

Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
Z
zengyawen 已提交
224

A
Annie_wang 已提交
225
This API uses a synchronous mode.
Z
zengyawen 已提交
226

A
annie_wangli 已提交
227 228
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
229
**Parameters**
A
annie_wangli 已提交
230 231 232 233
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
234 235
**Example**
  ```js
A
annie_wangli 已提交
236 237 238 239 240 241 242 243 244 245
  dataStorage.removeStorageFromCacheSync(path + '/mystore')
  ```


## dataStorage.removeStorageFromCache

removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void

Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.

A
Annie_wang 已提交
246
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
247 248 249

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
250
**Parameters**
A
annie_wangli 已提交
251 252 253 254 255
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
256 257
**Example**
  ```js
A
annie_wangli 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  dataStorage.removeStorageFromCache(path + '/mystore', function (err) {
      if (err) {
          console.info("Removed storage from cache failed with err: " + err)
          return
      }
      console.info("Removed storage from cache successfully.")
  })
  ```


## dataStorage.removeStorageFromCache

removeStorageFromCache(path: string): Promise&lt;void&gt;

Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.

A
Annie_wang 已提交
274
This API uses a promise to return the result.
A
annie_wangli 已提交
275 276 277

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
278
**Parameters**
A
annie_wangli 已提交
279 280 281 282
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
283
**Return value**
A
annie_wangli 已提交
284 285 286 287
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
288 289
**Example**
  ```js
A
annie_wangli 已提交
290 291 292 293 294 295 296 297 298 299
  let promiserevSt = dataStorage.removeStorageFromCache(path + '/mystore')
  promiserevSt.then(() => {
      console.info("Removed storage from cache successfully.")
  }).catch((err) => {
      console.info("Removed storage from cache failed with err: " + err)
  })
  ```


## Storage
Z
zengyawen 已提交
300 301 302 303

Provides APIs for obtaining and modifying storage data.


A
annie_wangli 已提交
304 305 306
### getSync

getSync(key: string, defValue: ValueType): ValueType
Z
zengyawen 已提交
307 308 309

Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.

A
Annie_wang 已提交
310
This API uses a synchronous mode.
Z
zengyawen 已提交
311

A
annie_wangli 已提交
312 313
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
314
**Parameters**
A
annie_wangli 已提交
315 316 317
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
Annie_wang 已提交
318
  | 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.|
A
annie_wangli 已提交
319

A
Annie_wang 已提交
320
**Return value**
A
annie_wangli 已提交
321 322 323 324
  | Type| Description|
  | -------- | -------- |
  | ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.|

A
Annie_wang 已提交
325 326
**Example**
  ```js
A
annie_wangli 已提交
327 328 329 330 331 332 333 334
  let value = storage.getSync('startup', 'default')
  console.info("The value of startup is " + value)
  ```


### get

get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void
Z
zengyawen 已提交
335 336 337

Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.

A
Annie_wang 已提交
338
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
339 340 341

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
342
**Parameters**
A
annie_wangli 已提交
343 344 345
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
Annie_wang 已提交
346
  | defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
347 348
  | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
349 350
**Example**
  ```js
A
annie_wangli 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363
  storage.get('startup', 'default', function(err, value) {
      if (err) {
          console.info("Get the value of startup failed with err: " + err)
          return
      }
      console.info("The value of startup is " + value)
  })
  ```


### get

get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
Z
zengyawen 已提交
364 365 366

Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.

A
Annie_wang 已提交
367
This API uses a promise to return the result.
A
annie_wangli 已提交
368 369 370

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
371 372 373 374 375 376
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
377

A
Annie_wang 已提交
378
**Return value**
A
annie_wangli 已提交
379 380 381 382
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;ValueType&gt; | Promise used to return the result.|

A
Annie_wang 已提交
383 384
**Example**
  ```js
A
annie_wangli 已提交
385 386 387 388 389 390 391
  let promiseget = storage.get('startup', 'default')
  promiseget.then((value) => {
      console.info("The value of startup is " + value)
  }).catch((err) => {
      console.info("Get the value of startup failed with err: " + err)
  })
  ```
Z
zengyawen 已提交
392 393


A
annie_wangli 已提交
394 395 396 397 398
### putSync

putSync(key: string, value: ValueType): void

Obtains 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()**.
Z
zengyawen 已提交
399

A
Annie_wang 已提交
400
This API uses a synchronous mode.
Z
zengyawen 已提交
401

A
annie_wangli 已提交
402 403
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
404
**Parameters**
A
annie_wangli 已提交
405 406
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
407 408
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
409

A
Annie_wang 已提交
410 411
**Example**
  ```js
A
annie_wangli 已提交
412 413 414 415 416 417 418 419 420 421
  storage.putSync('startup', 'auto')
  ```


### put

put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void

Obtains 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()**.

A
Annie_wang 已提交
422
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
423 424 425

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
426
**Parameters**
A
annie_wangli 已提交
427 428
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
429 430
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
431 432
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
433 434
**Example**
  ```js
A
annie_wangli 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  storage.put('startup', 'auto', function (err) {
      if (err) {
          console.info("Put the value of startup failed with err: " + err)
          return
      }
      console.info("Put the value of startup successfully.")
  })
  ```


### put

put(key: string, value: ValueType): Promise&lt;void&gt;

Obtains 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()**.

A
Annie_wang 已提交
451
This API uses a promise to return the result.
A
annie_wangli 已提交
452 453 454

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
455
**Parameters**
A
annie_wangli 已提交
456 457
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
458 459
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
460

A
Annie_wang 已提交
461
**Return value**
A
annie_wangli 已提交
462 463 464 465
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
466 467
**Example**
  ```js
A
annie_wangli 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481
  let promiseput = storage.put('startup', 'auto')
  promiseput.then(() => {
      console.info("Put the value of startup successfully.")
  }).catch((err) => {
      console.info("Put the value of startup failed with err: " + err)
  })
  ```


### hasSync

hasSync(key: string): boolean

Checks whether the storage object contains data with a given key.
Z
zengyawen 已提交
482

A
Annie_wang 已提交
483
This API uses a synchronous mode.
Z
zengyawen 已提交
484

A
annie_wangli 已提交
485 486
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
487
**Parameters**
A
annie_wangli 已提交
488 489 490 491
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
492
**Return value**
A
annie_wangli 已提交
493 494 495 496
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|

A
Annie_wang 已提交
497 498
**Example**
  ```js
A
annie_wangli 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511
  let isExist = storage.hasSync('startup')
  if (isExist) {
      console.info("The key of startup is contained.")
  }
  ```


### has

has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean

Checks whether the storage object contains data with a given key.

A
Annie_wang 已提交
512
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
513 514 515

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
516
**Parameters**
A
annie_wangli 已提交
517 518 519 520 521
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
522
**Return value**
A
annie_wangli 已提交
523 524 525 526
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|

A
Annie_wang 已提交
527 528
**Example**
  ```js
A
annie_wangli 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  storage.has('startup', function (err, isExist) {
      if (err) {
          console.info("Check the key of startup failed with err: " + err)
          return
      }
      if (isExist) {
          console.info("The key of startup is contained.")
      }
  })
  ```


### has

has(key: string): Promise&lt;boolean&gt;

Checks whether the storage object contains data with a given key.

A
Annie_wang 已提交
547
This API uses a promise to return the result.
A
annie_wangli 已提交
548 549 550

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
551
**Parameters**
A
annie_wangli 已提交
552 553 554 555
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
556
**Return value**
A
annie_wangli 已提交
557 558 559 560
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise used to return the result.|

A
Annie_wang 已提交
561 562
**Example**
  ```js
A
annie_wangli 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  let promisehas = storage.has('startup')
  promisehas.then((isExist) => {
      if (isExist) {
          console.info("The key of startup is contained.")
      }
  }).catch((err) => {
      console.info("Check the key of startup failed with err: " + err)
  })
  ```


### deleteSync

deleteSync(key: string): void

Deletes data with the specified key from this storage object.
Z
zengyawen 已提交
579

A
Annie_wang 已提交
580
This API uses a synchronous mode.
Z
zengyawen 已提交
581

A
annie_wangli 已提交
582 583
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
584
**Parameters**
A
annie_wangli 已提交
585 586 587 588
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
589 590
**Example**
  ```js
A
annie_wangli 已提交
591 592 593 594
  storage.deleteSync('startup')
  ```


A
Annie_wang 已提交
595
### deletej
A
annie_wangli 已提交
596 597 598 599 600

delete(key: string, callback: AsyncCallback&lt;void&gt;): void

Deletes data with the specified key from this storage object.

A
Annie_wang 已提交
601
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
602 603 604

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
605
**Parameters**
A
annie_wangli 已提交
606 607 608 609 610
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
611 612
**Example**
  ```js
A
annie_wangli 已提交
613 614 615 616 617 618 619 620 621
  storage.delete('startup', function (err) {
      if (err) {
          console.info("Delete startup key failed with err: " + err)
          return
      }
      console.info("Deleted startup key successfully.")
  })
  ```

Z
zengyawen 已提交
622

A
annie_wangli 已提交
623
### delete
Z
zengyawen 已提交
624

A
annie_wangli 已提交
625
delete(key: string): Promise&lt;void&gt;
Z
zengyawen 已提交
626

A
annie_wangli 已提交
627
Deletes data with the specified key from this storage object.
Z
zengyawen 已提交
628

A
Annie_wang 已提交
629
This API uses a promise to return the result.
Z
zengyawen 已提交
630

A
annie_wangli 已提交
631
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
632

A
Annie_wang 已提交
633
**Parameters**
A
annie_wangli 已提交
634 635 636
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data.|
Z
zengyawen 已提交
637

A
Annie_wang 已提交
638
**Return value**
A
annie_wangli 已提交
639 640 641
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|
Z
zengyawen 已提交
642

A
Annie_wang 已提交
643 644
**Example**
  ```js
A
annie_wangli 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  let promisedel = storage.delete('startup')
  promisedel.then(() => {
      console.info("Deleted startup key successfully.")
  }).catch((err) => {
      console.info("Delete startup key failed with err: " + err)
  })
  ```


### flushSync

flushSync(): void

Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.

A
Annie_wang 已提交
660
This API uses a synchronous mode.
Z
zengyawen 已提交
661

A
annie_wangli 已提交
662
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
663

A
Annie_wang 已提交
664 665
**Example**
  ```js
A
annie_wangli 已提交
666 667 668 669 670
  storage.flushSync()
  ```


### flush
Z
zengyawen 已提交
671

A
annie_wangli 已提交
672
flush(callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
673

A
annie_wangli 已提交
674
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
Z
zengyawen 已提交
675

A
Annie_wang 已提交
676
This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
677

A
annie_wangli 已提交
678
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
679

A
Annie_wang 已提交
680
**Parameters**
A
annie_wangli 已提交
681 682 683
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
Z
zengyawen 已提交
684

A
Annie_wang 已提交
685 686
**Example**
  ```js
A
annie_wangli 已提交
687 688 689 690 691 692 693 694
  storage.flush(function (err) {
      if (err) {
          console.info("Flush to file failed with err: " + err)
          return
      }
      console.info("Flushed to file successfully.")
  })
  ```
Z
zengyawen 已提交
695 696


A
annie_wangli 已提交
697
### flush
Z
zengyawen 已提交
698

A
annie_wangli 已提交
699
flush(): Promise&lt;void&gt;
Z
zengyawen 已提交
700

A
annie_wangli 已提交
701
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
Z
zengyawen 已提交
702

A
Annie_wang 已提交
703
This API uses a promise to return the result.
Z
zengyawen 已提交
704

A
annie_wangli 已提交
705
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
706

A
Annie_wang 已提交
707
**Return value**
A
annie_wangli 已提交
708 709 710 711
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
712 713
**Example**
  ```js
A
annie_wangli 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727
  let promiseflush = storage.flush()
  promiseflush.then(() => {
      console.info("Flushed to file successfully.")
  }).catch((err) => {
      console.info("Flush to file failed with err: " + err)
  })
  ```


### clearSync

clearSync(): void

Clears this **Storage** object.
Z
zengyawen 已提交
728

A
Annie_wang 已提交
729
This API uses a synchronous mode.
Z
zengyawen 已提交
730

A
annie_wangli 已提交
731 732
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
733 734
**Example**
  ```js
A
annie_wangli 已提交
735 736 737 738 739 740 741 742 743 744
  storage.clearSync()
  ```


### clear

clear(callback: AsyncCallback&lt;void&gt;): void

Clears this **Storage** object.

A
Annie_wang 已提交
745
This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
746 747 748

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
749
**Parameters**
A
annie_wangli 已提交
750 751 752
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
Z
zengyawen 已提交
753

A
Annie_wang 已提交
754 755
**Example**
  ```js
A
annie_wangli 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
  storage.clear(function (err) {
      if (err) {
          console.info("Clear to file failed with err: " + err)
          return
      }
      console.info("Cleared to file successfully.")
  })
  ```


### clear

clear(): Promise&lt;void&gt;

Clears this **Storage** object.

A
Annie_wang 已提交
772
This API uses a promise to return the result.
A
annie_wangli 已提交
773 774 775

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
776
**Return value**
A
annie_wangli 已提交
777 778 779 780
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
781 782
**Example**
  ```js
A
annie_wangli 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796
  let promiseclear = storage.clear()
  promiseclear.then(() => {
      console.info("Cleared to file successfully.")
  }).catch((err) => {
      console.info("Clear to file failed with err: " + err)
  })
  ```


### on('change')

on(type: 'change', callback: Callback&lt;StorageObserver&gt;): void

Subscribes 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.
Z
zengyawen 已提交
797

A
annie_wangli 已提交
798 799
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
800
**Parameters**
A
annie_wangli 已提交
801 802 803 804 805
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|

A
Annie_wang 已提交
806 807
**Example**
  ```js
A
annie_wangli 已提交
808 809 810 811 812 813 814 815 816 817 818 819
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  storage.on('change', observer)
  storage.putSync('startup', 'auto')
  storage.flushSync()  // observer will be called.
  ```


### off('change')

off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
Z
zengyawen 已提交
820 821 822

Unsubscribes from data changes.

A
annie_wangli 已提交
823 824
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
825
**Parameters**
A
annie_wangli 已提交
826 827 828 829 830
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|

A
Annie_wang 已提交
831 832
**Example**
  ```js
A
annie_wangli 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  storage.off('change', observer)
  ```


## StorageObserver

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | No| Data changed.|
A
Annie_wang 已提交
847 848 849 850 851 852 853 854 855 856 857 858

## ValueType

Enumerates the value types.

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name   | Description                |
| ------- | -------------------- |
| number  | The value is a number.  |
| string  | The value is a string.  |
| boolean | The value is of Boolean type.|