js-apis-data-preferences.md 21.3 KB
Newer Older
A
Annie_wang 已提交
1
# Preferences
A
annie_wangli 已提交
2

A
Annie_wang 已提交
3
Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support lightweight data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type.
A
annie_wangli 已提交
4 5


A
Annie_wang 已提交
6
> **NOTE**<br/>
A
Annie_wang 已提交
7
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
annie_wangli 已提交
8 9


A
Annie_wang 已提交
10
## Modules to Import
A
annie_wangli 已提交
11

A
Annie_wang 已提交
12 13
```ts
import data_preferences from '@ohos.data.preferences';
A
annie_wangli 已提交
14 15
```

A
Annie_wang 已提交
16
## Constants
A
annie_wangli 已提交
17

A
Annie_wang 已提交
18
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
19

A
Annie_wang 已提交
20
| Name| Type| Readable| Writable| Description|
A
annie_wangli 已提交
21
| -------- | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
22 23
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It must be less than 80 bytes.|
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.|
A
annie_wangli 已提交
24 25


A
annie_wangli 已提交
26
## data_preferences.getPreferences
A
annie_wangli 已提交
27 28 29

getPreferences(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void

A
Annie_wang 已提交
30
Reads a **Preferences** persistence file and loads data to the **Preferences** instance for data operations. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
31 32


A
Annie_wang 已提交
33
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
34

A
Annie_wang 已提交
35
**Parameters**
A
Annie_wang 已提交
36 37 38 39 40
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
41

A
Annie_wang 已提交
42
**Example**
A
Annie_wang 已提交
43 44 45
```ts
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
    if (err) {
A
Annie_wang 已提交
46
              console.info("Failed to get the preferences")
A
Annie_wang 已提交
47 48 49 50 51
        return;
    }
    console.info("Got preferences successfully.")
})
```
A
annie_wangli 已提交
52 53


A
annie_wangli 已提交
54
## data_preferences.getPreferences
A
annie_wangli 已提交
55 56 57

getPreferences(context: Context, name: string): Promise&lt;Preferences&gt;

A
Annie_wang 已提交
58
Reads a **Preferences** persistence file and loads data to the **Preferences** instance for data operations. This API uses a promise to return the result.
A
annie_wangli 已提交
59

A
Annie_wang 已提交
60
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
61

A
Annie_wang 已提交
62
**Parameters**
A
Annie_wang 已提交
63 64 65 66
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
67

A
Annie_wang 已提交
68
**Return value**
A
Annie_wang 已提交
69 70 71
| Type| Description|
| -------- | -------- |
| Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
72

A
Annie_wang 已提交
73
**Example**
A
Annie_wang 已提交
74 75 76 77 78
```ts
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
    console.info("Got preferences successfully.")
}).catch((err) => {
A
Annie_wang 已提交
79
    console.info("Failed to get the preferences")
A
Annie_wang 已提交
80 81
})
```
A
annie_wangli 已提交
82 83


A
annie_wangli 已提交
84
## data_preferences.deletePreferences
A
annie_wangli 已提交
85 86 87

deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
88
Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
A
Annie_wang 已提交
89
Once a **Preferences** persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
90

A
Annie_wang 已提交
91
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
92

A
Annie_wang 已提交
93
**Parameters**
A
Annie_wang 已提交
94 95 96 97 98
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
99

A
Annie_wang 已提交
100
**Example**
A
Annie_wang 已提交
101 102 103
```ts
data_preferences.deletePreferences(this.context, 'mystore', function (err) {
    if (err) {
A
Annie_wang 已提交
104
              console.info("Failed to delete data, err: " + err)
A
Annie_wang 已提交
105 106 107 108 109
        return
    }
    console.info("Deleted preferences successfully.")
})
```
A
annie_wangli 已提交
110 111


A
annie_wangli 已提交
112
## data_preferences.deletePreferences
A
annie_wangli 已提交
113 114 115

deletePreferences(context: Context, name: string): Promise&lt;void&gt;

A
Annie_wang 已提交
116
Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
A
Annie_wang 已提交
117
Once a **Preferences** persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result.
A
annie_wangli 已提交
118

A
Annie_wang 已提交
119
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
120

A
Annie_wang 已提交
121
**Parameters**
A
Annie_wang 已提交
122 123 124 125
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
126

A
Annie_wang 已提交
127
**Return value**
A
Annie_wang 已提交
128 129 130
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
131

A
Annie_wang 已提交
132
**Example**
A
Annie_wang 已提交
133 134 135 136 137 138 139 140
```ts
let promise = data_preferences.deletePreferences(this.context, 'mystore')
promise.then(() => {
    console.info("Deleted preferences successfully.")
}).catch((err) => {
          console.info("Failed to delete preferences, err: " + err)
})
```
A
annie_wangli 已提交
141 142


A
annie_wangli 已提交
143
## data_preferences.removePreferencesFromCache
A
annie_wangli 已提交
144 145 146

removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
147
Removes a **Preferences** singleton instance from the memory.
A
annie_wangli 已提交
148

A
Annie_wang 已提交
149
When a **Preferences** singleton instance is removed, this instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
150

A
Annie_wang 已提交
151
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
152

A
Annie_wang 已提交
153
**Parameters**
A
Annie_wang 已提交
154 155 156 157 158
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
159

A
Annie_wang 已提交
160
**Example**
A
Annie_wang 已提交
161 162 163 164 165 166 167 168 169
```ts
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
    if (err) {
        console.info("Failed to remove preferences from cache, err: " + err)
        return
    }
    console.info("Removed preferences from cache successfully.")
})
```
A
annie_wangli 已提交
170 171


A
annie_wangli 已提交
172
## data_preferences.removePreferencesFromCache
A
annie_wangli 已提交
173 174 175

removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;

A
Annie_wang 已提交
176
Removes a **Preferences** singleton instance from the memory.
A
annie_wangli 已提交
177

A
Annie_wang 已提交
178
When a **Preferences** singleton instance is removed, this 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 已提交
179

A
Annie_wang 已提交
180
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
181

A
Annie_wang 已提交
182 183 184 185 186
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
187

A
Annie_wang 已提交
188
**Return value**
A
Annie_wang 已提交
189 190 191
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
192

A
Annie_wang 已提交
193
**Example**
A
Annie_wang 已提交
194 195 196 197 198 199 200 201
```ts
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
promise.then(() => {
    console.info("Removed preferences from cache successfully.")
}).catch((err) => {
    console.info("Failed to remove preferences from cache, err: " + err)
})
```
A
annie_wangli 已提交
202 203 204 205


## Preferences

A
Annie_wang 已提交
206
Provides APIs for obtaining and modifying storage data.
A
annie_wangli 已提交
207 208 209 210 211 212


### get

get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void

A
Annie_wang 已提交
213
Obtains the value of a key. If the value is null or a non-default value, the default data is returned. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
214

A
Annie_wang 已提交
215
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
216

A
Annie_wang 已提交
217
**Parameters**
A
Annie_wang 已提交
218 219 220 221 222
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to obtain. It cannot be empty.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
223

A
Annie_wang 已提交
224
**Example**
A
Annie_wang 已提交
225
```ts
A
Annie_wang 已提交
226
preferences.get('startup', 'default', function(err, value) {
A
Annie_wang 已提交
227
    if (err) {
A
Annie_wang 已提交
228
                  console.info("Failed to get the value of startup, err: " + err)
A
Annie_wang 已提交
229 230
        return
    }
A
Annie_wang 已提交
231
              console.info("The value of startup is " + value)
A
Annie_wang 已提交
232 233
})
```
A
annie_wangli 已提交
234 235 236 237 238 239


### get

get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;

A
Annie_wang 已提交
240
Obtains the value of a key. If the value is null or a non-default value, the default data is returned. This API uses a promise to return the result.
A
annie_wangli 已提交
241

A
Annie_wang 已提交
242
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
243

A
Annie_wang 已提交
244
**Parameters**
A
Annie_wang 已提交
245 246 247 248
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to obtain. 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 已提交
249

A
Annie_wang 已提交
250
**Return value**
A
Annie_wang 已提交
251 252 253
| Type| Description|
| -------- | -------- |
| Promise&lt;ValueType&gt; | Promise used to return the result.|
A
annie_wangli 已提交
254

A
Annie_wang 已提交
255
**Example**
A
Annie_wang 已提交
256 257 258 259 260
```ts
let promise = preferences.get('startup', 'default')
promise.then((value) => {
    console.info("The value of startup is " + value)
}).catch((err) => {
A
Annie_wang 已提交
261
              console.info("Failed to get the value of startup, err: " + err)
A
Annie_wang 已提交
262 263
})
```
A
annie_wangli 已提交
264

A
Annie_wang 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
### getAll

getAll(callback: AsyncCallback&lt;Object&gt;): void;

Obtains the **Object** instance that contains all KV pairs.

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

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;Object&gt; | Yes| Callback used to return the **Object** instance obtained.|

**Example**
```ts
preferences.get.getAll(function (err, value) {
    if (err) {
        console.info("getAll failed, err: " + err)
        return
    }
    let keys = Object.keys(value)
    console.info('getAll keys = ' + keys)
    console.info("getAll object = " + JSON.stringify(value))
});
```


### getAll

getAll(): Promise&lt;Object&gt;

Obtains the **Object** instance that contains all KV pairs.

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

**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;Object&gt; | Promise used to return the **Object** instance obtained.|

**Example**
```ts
let promise = preferences.getAll()
promise.then((value) => {
    let keys = Object.keys(value)
    console.info('getAll keys = ' + keys)
    console.info("getAll object = " + JSON.stringify(value))
}).catch((err) => {
    console.info("getAll failed, err: " + err)
})
```
A
annie_wangli 已提交
316 317 318 319 320

### put

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

A
Annie_wang 已提交
321
Puts a new value to this **Preferences** instance and its persistence file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
322

A
Annie_wang 已提交
323
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
324

A
Annie_wang 已提交
325
**Parameters**
A
Annie_wang 已提交
326 327 328 329 330
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
331

A
Annie_wang 已提交
332
**Example**
A
Annie_wang 已提交
333 334 335
```ts
preferences.put('startup', 'auto', function (err) {
    if (err) {
A
Annie_wang 已提交
336
                  console.info("Failed to put the value of startup, err: " + err)
A
Annie_wang 已提交
337 338 339 340 341
        return
    }
    console.info("Put the value of startup successfully.")
})
```
A
annie_wangli 已提交
342 343 344 345 346 347


### put

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

A
Annie_wang 已提交
348
Puts a new value to this **Preferences** instance and its persistence file. This API uses a promise to return the result.
A
annie_wangli 已提交
349

A
Annie_wang 已提交
350
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
351

A
Annie_wang 已提交
352
**Parameters**
A
Annie_wang 已提交
353 354 355 356
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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 已提交
357

A
Annie_wang 已提交
358
**Return value**
A
Annie_wang 已提交
359 360 361
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
362

A
Annie_wang 已提交
363
**Example**
A
Annie_wang 已提交
364 365 366 367 368
```ts
let promise = preferences.put('startup', 'auto')
promise.then(() => {
    console.info("Put the value of startup successfully.")
}).catch((err) => {
A
Annie_wang 已提交
369
              console.info("Failed to put the value of startup, err: " + err)
A
Annie_wang 已提交
370 371
})
```
A
annie_wangli 已提交
372 373 374 375


### has

A
Annie_wang 已提交
376
has(key: string, callback: AsyncCallback&lt;boolean&gt;): void
A
annie_wangli 已提交
377

A
Annie_wang 已提交
378
Checks whether this **Preferences** instance contains data with a given key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
379

A
Annie_wang 已提交
380
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
381

A
Annie_wang 已提交
382
**Parameters**
A
Annie_wang 已提交
383 384 385 386
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to check. It cannot be empty.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
A
annie_wangli 已提交
387

A
Annie_wang 已提交
388
**Example**
A
Annie_wang 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401
```ts
preferences.has('startup', function (err, isExist) {
    if (err) {
        console.info("Failed to check the key of startup, err: " + err)
        return
    }
    if (isExist) {
        console.info("The key of startup is contained.")
    } else {
        console.info("The key of startup is not contained.")
    }
})
```
A
annie_wangli 已提交
402 403 404 405 406 407


### has

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

A
Annie_wang 已提交
408
Checks whether this **Preferences** instance contains data with a given key. This API uses a promise to return the result.
A
annie_wangli 已提交
409

A
Annie_wang 已提交
410
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
411

A
Annie_wang 已提交
412
**Parameters**
A
Annie_wang 已提交
413 414 415
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to check. It cannot be empty.|
A
annie_wangli 已提交
416

A
Annie_wang 已提交
417
**Return value**
A
Annie_wang 已提交
418 419 420
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
A
annie_wangli 已提交
421

A
Annie_wang 已提交
422
**Example**
A
Annie_wang 已提交
423 424 425 426 427 428 429 430 431
```ts
let promise = preferences.has('startup')
promise.then((isExist) => {
    if (isExist) {
        console.info("The key of startup is contained.")
    } else {
        console.info("The key of startup is not contained.")
    }
}).catch((err) => {
A
Annie_wang 已提交
432
    console.info("Failed to check the key of startup, err: " + err)
A
Annie_wang 已提交
433 434
})
```
A
annie_wangli 已提交
435 436 437 438 439 440


### delete

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

A
Annie_wang 已提交
441
Deletes a KV pair of the specified key from this **Preferences** instance. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
442

A
Annie_wang 已提交
443
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
444

A
Annie_wang 已提交
445
**Parameters**
A
Annie_wang 已提交
446 447 448 449
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the KV pair to delete. It cannot be empty.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
450

A
Annie_wang 已提交
451
**Example**
A
Annie_wang 已提交
452 453 454 455 456 457 458 459 460
```ts
preferences.delete('startup', function (err) {
    if (err) {
        console.info("Failed to delete startup key, err: " + err)
        return
    }
    console.info("Deleted startup key successfully.")
})
```
A
annie_wangli 已提交
461 462 463 464 465 466


### delete

delete(key: string): Promise&lt;void&gt;

A
Annie_wang 已提交
467
Deletes a KV pair of the specified key from this **Preferences** instance. This API uses a promise to return the result.
A
annie_wangli 已提交
468

A
Annie_wang 已提交
469
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
470

A
Annie_wang 已提交
471
**Parameters**
A
Annie_wang 已提交
472 473 474
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the KV pair to delete. It cannot be empty.|
A
annie_wangli 已提交
475

A
Annie_wang 已提交
476
**Return value**
A
Annie_wang 已提交
477 478 479
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
480

A
Annie_wang 已提交
481
**Example**
A
Annie_wang 已提交
482 483 484 485 486 487 488 489
```ts
let promise = preferences.delete('startup')
promise.then(() => {
    console.info("Deleted startup key successfully.")
}).catch((err) => {
    console.info("Failed to delete startup key, err: " + err)
})
```
A
annie_wangli 已提交
490 491 492 493 494 495


### flush

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

A
Annie_wang 已提交
496
Saves the modification to this **Preferences** instance and synchronizes the modification to the **Preferences** persistence file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
497

A
Annie_wang 已提交
498
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
499

A
Annie_wang 已提交
500
**Parameters**
A
Annie_wang 已提交
501 502 503
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
504

A
Annie_wang 已提交
505
**Example**
A
Annie_wang 已提交
506 507 508 509 510 511 512 513 514
```ts
preferences.flush(function (err) {
    if (err) {
        console.info("Failed to flush data to file, err: " + err)
        return
    }
    console.info("Flushed data to file successfully.")
})
```
A
annie_wangli 已提交
515 516 517 518 519 520


### flush

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

A
Annie_wang 已提交
521
Saves the modification to this **Preferences** instance and synchronizes the modification to the **Preferences** persistence file. This API uses a promise to return the result.
A
annie_wangli 已提交
522

A
Annie_wang 已提交
523
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
524

A
Annie_wang 已提交
525
**Return value**
A
Annie_wang 已提交
526 527 528
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
529

A
Annie_wang 已提交
530
**Example**
A
Annie_wang 已提交
531 532 533 534 535 536 537 538
```ts
let promise = preferences.flush()
promise.then(() => {
    console.info("Flushed data to file successfully.")
}).catch((err) => {
    console.info("Failed to flush data to file, err: " + err)
})
```
A
annie_wangli 已提交
539 540 541 542 543 544


### clear

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

A
Annie_wang 已提交
545
Clears data of this **Preferences** instance. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
546

A
Annie_wang 已提交
547
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
548

A
Annie_wang 已提交
549 550 551 552
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
553

A
Annie_wang 已提交
554
**Example**
A
Annie_wang 已提交
555 556 557 558 559 560 561 562 563
```ts
preferences.clear(function (err) {
    if (err) {
        console.info("Failed to clear data, err: " + err)
        return
    }
    console.info("Cleared to file successfully.")
})
```
A
annie_wangli 已提交
564 565 566 567 568 569


### clear

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

A
Annie_wang 已提交
570
Clears data of this **Preferences** instance. This API uses a promise to return the result.
A
annie_wangli 已提交
571

A
Annie_wang 已提交
572
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
573

A
Annie_wang 已提交
574
**Return value**
A
Annie_wang 已提交
575 576 577
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
578

A
Annie_wang 已提交
579
**Example**
A
Annie_wang 已提交
580 581 582 583 584 585 586 587
```ts
let promise = preferences.clear()
promise.then(() => {
    console.info("Cleared to file successfully.")
}).catch((err) => {
    console.info("Failed to clear data, err: " + err)
})
```
A
annie_wangli 已提交
588 589 590 591 592 593


### on('change')

on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void

A
Annie_wang 已提交
594
Subscribes to data changes. When the value of the subscribed key changes, a callback will be invoked after **flush()** is executed.
A
annie_wangli 已提交
595

A
Annie_wang 已提交
596
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
597

A
Annie_wang 已提交
598
**Parameters**
A
Annie_wang 已提交
599 600 601 602
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes|  Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt;  | Yes| Callback used to return data changes.|
A
annie_wangli 已提交
603

A
Annie_wang 已提交
604
**Example**
A
Annie_wang 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
```ts
var observer = function (key) {
    console.info("The key of " + key + " changed.")
}

...

preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
    if (err) {
        console.info("Failed to put the value of startup, err: " + err)
        return
    }
    console.info("Put the value of startup successfully.")

    preferences.flush(function (err) {
        if (err) {
            console.info("Failed to flush data to file, err: " + err)
            return
        }
        console.info("Flushed data to file successfully.")    // The observer will be called.
    })
})
```
A
annie_wangli 已提交
629 630 631 632


### off('change')

A
Annie_wang 已提交
633
off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void
A
annie_wangli 已提交
634

A
Annie_wang 已提交
635
Unsubscribes from data changes.
A
annie_wangli 已提交
636

A
Annie_wang 已提交
637
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
638

A
Annie_wang 已提交
639
**Parameters**
A
Annie_wang 已提交
640 641 642 643
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt;  | No| Callback used to return data changes. If this parameter is left empty, all callbacks for data changes will be canceled.|
A
annie_wangli 已提交
644

A
Annie_wang 已提交
645
**Example**
A
Annie_wang 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
```ts
var observer = function (key) {
    console.info("The key of " + key + " changed.")
}

...

preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
    if (err) {
        console.info("Failed to put the value of startup, err: " + err)
        return
    }
    console.info("Put the value of startup successfully.")

    preferences.flush(function (err) {
        if (err) {
            console.info("Failed to flush data to file, err: " + err)
            return
        }
        console.info("Flushed data to file successfully.")    // The observer will be called.
        preferences.off('change', observer)
    })
})
```
A
Annie_wang 已提交
671 672 673

## ValueType

A
Annie_wang 已提交
674
Enumerates the value types.
A
Annie_wang 已提交
675

A
Annie_wang 已提交
676
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
Annie_wang 已提交
677

A
Annie_wang 已提交
678
| Name   | Description                |
A
Annie_wang 已提交
679
| ------- | -------------------- |
A
Annie_wang 已提交
680 681 682
| number  | The value is a number.  |
| string  | The value is a string.  |
| boolean | The value is of Boolean type.|