js-apis-data-preferences.md 20.0 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 supports 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 is 80 bytes.|
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It is 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
37
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
38
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
39
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
Annie_wang 已提交
40
  | 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
64
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
65
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
66
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
67

A
Annie_wang 已提交
68
**Return value**
A
Annie_wang 已提交
69
  | Type| Description|
A
annie_wangli 已提交
70
  | -------- | -------- |
A
Annie_wang 已提交
71
  | 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
95
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
96
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
97
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
Annie_wang 已提交
98
  | 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
123
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
124
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
125
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
126

A
Annie_wang 已提交
127
**Return value**
A
Annie_wang 已提交
128
  | Type| Description|
A
annie_wangli 已提交
129
  | -------- | -------- |
A
Annie_wang 已提交
130
  | 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
155
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
156
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
157
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
Annie_wang 已提交
158
  | 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
Parameters
A
Annie_wang 已提交
183
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
184
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
185
  | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
A
Annie_wang 已提交
186
  | name | string | Yes| Name of the **Preferences** instance persistence file.|
A
annie_wangli 已提交
187

A
Annie_wang 已提交
188
**Return value**
A
Annie_wang 已提交
189
  | Type| Description|
A
annie_wangli 已提交
190
  | -------- | -------- |
A
Annie_wang 已提交
191
  | 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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
219
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
220 221
  | 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_wang 已提交
222
  | 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 226 227
```ts
 preferences.get('startup', 'default', function(err, value) {
    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
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
246
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
247 248
  | 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
  | Type| Description|
A
annie_wangli 已提交
252
  | -------- | -------- |
A
Annie_wang 已提交
253
  | 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 265 266 267 268 269


### put

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

A
Annie_wang 已提交
270
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 已提交
271

A
Annie_wang 已提交
272
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
273

A
Annie_wang 已提交
274
**Parameters**
A
Annie_wang 已提交
275
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
276
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
277 278
  | 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_wang 已提交
279
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
280

A
Annie_wang 已提交
281
**Example**
A
Annie_wang 已提交
282 283 284
```ts
preferences.put('startup', 'auto', function (err) {
    if (err) {
A
Annie_wang 已提交
285
                  console.info("Failed to put the value of startup, err: " + err)
A
Annie_wang 已提交
286 287 288 289 290
        return
    }
    console.info("Put the value of startup successfully.")
})
```
A
annie_wangli 已提交
291 292 293 294 295 296


### put

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

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

A
Annie_wang 已提交
299
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
300

A
Annie_wang 已提交
301
**Parameters**
A
Annie_wang 已提交
302
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
303
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
304 305
  | 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 已提交
306

A
Annie_wang 已提交
307
**Return value**
A
Annie_wang 已提交
308
  | Type| Description|
A
annie_wangli 已提交
309
  | -------- | -------- |
A
Annie_wang 已提交
310
  | Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
311

A
Annie_wang 已提交
312
**Example**
A
Annie_wang 已提交
313 314 315 316 317
```ts
let promise = preferences.put('startup', 'auto')
promise.then(() => {
    console.info("Put the value of startup successfully.")
}).catch((err) => {
A
Annie_wang 已提交
318
              console.info("Failed to put the value of startup, err: " + err)
A
Annie_wang 已提交
319 320
})
```
A
annie_wangli 已提交
321 322 323 324 325 326


### has

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

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

A
Annie_wang 已提交
329
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
330

A
Annie_wang 已提交
331
**Parameters**
A
Annie_wang 已提交
332
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
333
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
334
  | key | string | Yes| Key of the data to check. It cannot be empty.|
A
Annie_wang 已提交
335
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
336

A
Annie_wang 已提交
337
**Return value**
A
Annie_wang 已提交
338
  | Type| Description|
A
annie_wangli 已提交
339
  | -------- | -------- |
A
Annie_wang 已提交
340
  | boolean | Returns **true** if the **Preferences** instance contains data with the specified key; returns **false** otherwise.|
A
annie_wangli 已提交
341

A
Annie_wang 已提交
342
**Example**
A
Annie_wang 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355
```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 已提交
356 357 358 359 360 361


### has

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

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

A
Annie_wang 已提交
364
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
365

A
Annie_wang 已提交
366
**Parameters**
A
Annie_wang 已提交
367
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
368
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
369
  | key | string | Yes| Key of the data to check. It cannot be empty.|
A
annie_wangli 已提交
370

A
Annie_wang 已提交
371
**Return value**
A
Annie_wang 已提交
372
  | Type| Description|
A
annie_wangli 已提交
373
  | -------- | -------- |
A
Annie_wang 已提交
374
  | Promise&lt;boolean&gt; | Promise used to return the result.|
A
annie_wangli 已提交
375

A
Annie_wang 已提交
376
**Example**
A
Annie_wang 已提交
377 378 379 380 381 382 383 384 385
```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 已提交
386
              console.info("Check the key of startup failed, err: " + err)
A
Annie_wang 已提交
387 388
})
```
A
annie_wangli 已提交
389 390 391 392 393 394


### delete

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

A
Annie_wang 已提交
395
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 已提交
396

A
Annie_wang 已提交
397
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
398

A
Annie_wang 已提交
399
**Parameters**
A
Annie_wang 已提交
400
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
401
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
402
  | key | string | Yes| Key of the KV pair to delete. It cannot be empty.|
A
Annie_wang 已提交
403
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
404

A
Annie_wang 已提交
405
**Example**
A
Annie_wang 已提交
406 407 408 409 410 411 412 413 414
```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 已提交
415 416 417 418 419 420


### delete

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

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

A
Annie_wang 已提交
423
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
424

A
Annie_wang 已提交
425
**Parameters**
A
Annie_wang 已提交
426
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
427
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
428
  | key | string | Yes| Key of the KV pair to delete. It cannot be empty.|
A
annie_wangli 已提交
429

A
Annie_wang 已提交
430
**Return value**
A
Annie_wang 已提交
431
  | Type| Description|
A
annie_wangli 已提交
432
  | -------- | -------- |
A
Annie_wang 已提交
433
  | Promise&lt;void&gt; | Promise used to return the result.|
A
annie_wangli 已提交
434

A
Annie_wang 已提交
435
**Example**
A
Annie_wang 已提交
436 437 438 439 440 441 442 443
```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 已提交
444 445 446 447 448 449


### flush

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

A
Annie_wang 已提交
450
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 已提交
451

A
Annie_wang 已提交
452
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
453

A
Annie_wang 已提交
454
**Parameters**
A
Annie_wang 已提交
455
  | Name| Type| Mandatory| Description|
A
annie_wangli 已提交
456
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
457
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
A
annie_wangli 已提交
458

A
Annie_wang 已提交
459
**Example**
A
Annie_wang 已提交
460 461 462 463 464 465 466 467 468
```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 已提交
469 470 471 472 473 474


### flush

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

A
Annie_wang 已提交
475
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 已提交
476

A
Annie_wang 已提交
477
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
478

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

A
Annie_wang 已提交
484
**Example**
A
Annie_wang 已提交
485 486 487 488 489 490 491 492
```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 已提交
493 494 495 496 497 498


### clear

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

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

A
Annie_wang 已提交
501
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
502

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

A
Annie_wang 已提交
508
**Example**
A
Annie_wang 已提交
509 510 511 512 513 514 515 516 517
```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 已提交
518 519 520 521 522 523


### clear

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

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

A
Annie_wang 已提交
526
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
527

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

A
Annie_wang 已提交
533
**Example**
A
Annie_wang 已提交
534 535 536 537 538 539 540 541
```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 已提交
542 543 544 545 546 547


### on('change')

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

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

A
Annie_wang 已提交
550
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
551

A
Annie_wang 已提交
552
**Parameters**
A
Annie_wang 已提交
553
  | Name| Type| Description|
A
annie_wangli 已提交
554
  | -------- | -------- | -------- |
A
Annie_wang 已提交
555 556
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
A
annie_wangli 已提交
557

A
Annie_wang 已提交
558
**Example**
A
Annie_wang 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
```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 已提交
583 584 585 586 587 588


### off('change')

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

A
Annie_wang 已提交
589
Unsubscribes from data changes.
A
annie_wangli 已提交
590

A
Annie_wang 已提交
591
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
annie_wangli 已提交
592

A
Annie_wang 已提交
593
**Parameters**
A
Annie_wang 已提交
594
  | Name| Type| Description|
A
annie_wangli 已提交
595
  | -------- | -------- | -------- |
A
Annie_wang 已提交
596 597
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
A
annie_wangli 已提交
598

A
Annie_wang 已提交
599
**Example**
A
Annie_wang 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
```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 已提交
625 626 627

## ValueType

A
Annie_wang 已提交
628
Enumerates the value types.
A
Annie_wang 已提交
629

A
Annie_wang 已提交
630
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
A
Annie_wang 已提交
631

A
Annie_wang 已提交
632
| Name   | Description                |
A
Annie_wang 已提交
633
| ------- | -------------------- |
A
Annie_wang 已提交
634 635 636
| number  | The value is a number.  |
| string  | The value is a string.  |
| boolean | The value is of Boolean type.|