You need to sign in or sign up before continuing.
js-apis-data-preferences.md 26.5 KB
Newer Older
W
wangxiyue 已提交
1
# 首选项
P
PaDoBoo 已提交
2

3 4 5
首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。

数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。
P
PaDoBoo 已提交
6 7


P
PaDaBoo 已提交
8
> **说明:**
9
>
10
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
P
PaDoBoo 已提交
11 12 13 14


## 导入模块

15
```js
16
import data_preferences from '@ohos.data.preferences';
P
PaDoBoo 已提交
17 18
```

G
ge-yafang 已提交
19
## 常量
P
PaDoBoo 已提交
20

21
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
22

23 24 25 26
| 名称             | 参数类型 | 可读 | 可写 | 说明                                    |
| ---------------- | -------- | ---- | ---- | --------------------------------------- |
| MAX_KEY_LENGTH   | string   | 是   | 否   | Key的最大长度限制,需小于80个字节。     |
| MAX_VALUE_LENGTH | string   | 是   | 否   | Value的最大长度限制,需小于8192个字节。 |
P
PaDoBoo 已提交
27 28


29
## data_preferences.getPreferences
P
PaDoBoo 已提交
30

P
PaDaBoo 已提交
31
getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void
P
PaDoBoo 已提交
32

33
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,使用callback异步回调。
P
PaDaBoo 已提交
34

P
PaDaBoo 已提交
35
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
36

G
ge-yafang 已提交
37
**参数:**
38 39 40 41 42 43

| 参数名   | 类型                                             | 必填 | 说明                                                         |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| context  | [Context](js-apis-ability-context.md)            | 是   | 应用上下文。                                                 |
| name     | string                                           | 是   | 应用程序内部数据存储名称。                                   |
| callback | AsyncCallback<[Preferences](#preferences)> | 是   | 回调函数。当获取Preferences实例成功,err为undefined,object为获取的Preferences实例;否则err为错误码。 |
P
PaDoBoo 已提交
44

G
ge-yafang 已提交
45
**示例:**
46 47 48 49

```js
var preferences = null;
data_preferences.getPreferences(this.context, 'mystore', function (err, object) {
P
PaDaBoo 已提交
50
    if (err) {
51
        console.info("Failed to get preferences. Cause: " + err);
P
PaDaBoo 已提交
52 53
        return;
    }
54 55
    preferences = object;
    console.info("Succeeded in getting preferences.");
P
PaDaBoo 已提交
56 57
})
```
P
PaDoBoo 已提交
58 59


60
## data_preferences.getPreferences
P
PaDoBoo 已提交
61

P
PaDaBoo 已提交
62
getPreferences(context: Context, name: string): Promise<Preferences>
P
PaDoBoo 已提交
63

64
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,使用Promise异步回调。
P
PaDoBoo 已提交
65

P
PaDaBoo 已提交
66
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
67

G
ge-yafang 已提交
68
**参数:**
69 70 71 72
| 参数名  | 类型                                  | 必填 | 说明                       |
| ------- | ------------------------------------- | ---- | -------------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用上下文。               |
| name    | string                                | 是   | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
73

G
ge-yafang 已提交
74
**返回值:**
75 76 77
| 类型                                       | 说明                               |
| ------------------------------------------ | ---------------------------------- |
| Promise<[Preferences](#preferences)> | Promise对象,返回Preferences实例。 |
P
PaDoBoo 已提交
78

G
ge-yafang 已提交
79
**示例:**
80 81 82

```js
var preferences = null;
83
let promise = data_preferences.getPreferences(this.context, 'mystore')
84 85 86
promise.then((object) => {
    preferences = object;
    console.info("Succeeded in getting preferences.");
P
PaDaBoo 已提交
87
}).catch((err) => {
88
    console.info("Failed to get preferences. Cause: " + err);
P
PaDaBoo 已提交
89 90
})
```
P
PaDoBoo 已提交
91 92


93
## data_preferences.deletePreferences
P
PaDoBoo 已提交
94

95
deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void
P
PaDoBoo 已提交
96

97 98 99
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件,使用callback异步回调。

删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
100

P
PaDaBoo 已提交
101
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
102

G
ge-yafang 已提交
103
**参数:**
104 105 106 107 108
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context  | [Context](js-apis-ability-context.md) | 是   | 应用上下文。                                         |
| name     | string                                | 是   | 应用程序内部数据存储名称。                           |
| callback | AsyncCallback<void>             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
109

G
ge-yafang 已提交
110
**示例:**
111
```js
112
data_preferences.deletePreferences(this.context, 'mystore', function (err) {
P
PaDaBoo 已提交
113
    if (err) {
114
        console.info("Failed to delete preferences. Cause: " + err);
P
PaDaBoo 已提交
115 116
        return
    }
117
    console.info("Succeeded in deleting preferences." );
P
PaDaBoo 已提交
118 119
})
```
P
PaDoBoo 已提交
120 121


122
## data_preferences.deletePreferences
P
PaDoBoo 已提交
123

P
PaDaBoo 已提交
124
deletePreferences(context: Context, name: string): Promise<void>
P
PaDoBoo 已提交
125

126 127 128
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件,使用Promise异步回调。

删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
129

P
PaDaBoo 已提交
130
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
131

G
ge-yafang 已提交
132
**参数:**
133 134 135 136
| 参数名  | 类型                                  | 必填 | 说明                       |
| ------- | ------------------------------------- | ---- | -------------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用程序或功能的上下文。   |
| name    | string                                | 是   | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
137

G
ge-yafang 已提交
138
**返回值:**
139 140 141
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise<void> | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
142

G
ge-yafang 已提交
143
**示例:**
144
```js
145
let promise = data_preferences.deletePreferences(this.context, 'mystore')
P
PaDaBoo 已提交
146
promise.then(() => {
147
    console.info("Succeeded in deleting preferences.");
P
PaDaBoo 已提交
148
}).catch((err) => {
149
    console.info("Failed to delete preferences. Cause: " + err);
P
PaDaBoo 已提交
150 151
})
```
P
PaDoBoo 已提交
152 153


154
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
155

156
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void
P
PaDoBoo 已提交
157

158
从内存中移除指定首选项持久化文件对应的Preferences单实例,使用callback异步回调。
G
ge-yafang 已提交
159

160
移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
161

P
PaDaBoo 已提交
162
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
163

G
ge-yafang 已提交
164
**参数:**
165 166 167 168 169
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context  | [Context](js-apis-ability-context.md) | 是   | 应用程序或功能的上下文。                             |
| name     | string                                | 是   | 应用程序内部数据存储名称。                           |
| callback | AsyncCallback<void>             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
170

G
ge-yafang 已提交
171
**示例:**
172
```js
173
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
P
PaDaBoo 已提交
174
    if (err) {
175 176
        console.info("Failed to remove preferences. Cause: " + err);
        return;
P
PaDaBoo 已提交
177
    }
178
    console.info("Succeeded in removing preferences.");
P
PaDaBoo 已提交
179 180
})
```
P
PaDoBoo 已提交
181 182


183
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
184

P
PaDaBoo 已提交
185
removePreferencesFromCache(context: Context, name: string): Promise<void>
P
PaDoBoo 已提交
186

187
从内存中移除指定首选项持久化文件对应的Preferences单实例,使用Promise异步回调。
G
ge-yafang 已提交
188

189
移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
190

P
PaDaBoo 已提交
191
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
192

G
ge-yafang 已提交
193
**参数:**
194 195 196 197
| 参数名  | 类型                                  | 必填 | 说明                       |
| ------- | ------------------------------------- | ---- | -------------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用程序或功能的上下文。   |
| name    | string                                | 是   | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
198

G
ge-yafang 已提交
199
**返回值:**
200 201 202
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise<void> | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
203

G
ge-yafang 已提交
204
**示例:**
205
```js
206
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
P
PaDaBoo 已提交
207
promise.then(() => {
208
    console.info("Succeeded in removing preferences.");
P
PaDaBoo 已提交
209
}).catch((err) => {
210
    console.info("Failed to remove preferences. Cause: " + err);
P
PaDaBoo 已提交
211 212
})
```
P
PaDoBoo 已提交
213 214 215 216


## Preferences

217 218 219
存储实例,提供获取和修改存储数据的接口。

下列接口都需先使用[data_preferences.getPreferences](#data_preferencesgetpreferences)获取到Preferences实例,再通过此实例调用对应接口。
P
PaDoBoo 已提交
220 221 222 223 224 225


### get

get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void

226
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用callback异步回调。
P
PaDoBoo 已提交
227

P
PaDaBoo 已提交
228
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
229

G
ge-yafang 已提交
230
**参数:**
231 232 233 234 235
| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                                       | 是   | 要获取的存储Key名称,不能为空。                              |
| defValue | [ValueType](#valuetype)                      | 是   | 默认返回值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
| callback | AsyncCallback&lt;[ValueType](#valuetype)&gt; | 是   | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误码。 |
P
PaDoBoo 已提交
236

G
ge-yafang 已提交
237
**示例:**
238 239 240

```js
preferences.get('startup', 'default', function(err, data) {
P
PaDaBoo 已提交
241
    if (err) {
242 243
        console.info("Failed to get value of 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
244
    }
245
    console.info("Succeeded in getting value of 'startup'. Data: " + data);
P
PaDaBoo 已提交
246 247
})
```
P
PaDoBoo 已提交
248 249 250 251 252 253


### get

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

254
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用Promise异步回调。
P
PaDoBoo 已提交
255

P
PaDaBoo 已提交
256
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
257

258 259 260 261 262
 **参数:**
| 参数名   | 类型                    | 必填 | 说明                                                         |
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| key      | string                  | 是   | 要获取的存储Key名称,不能为空。                              |
| defValue | [ValueType](#valuetype) | 是   | 默认返回值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
P
PaDoBoo 已提交
263

G
ge-yafang 已提交
264
**返回值:**
265 266 267 268

| 类型                                | 说明                          |
| ----------------------------------- | ----------------------------- |
| Promise<[ValueType](#valuetype)&gt; | Promise对象,返回键对应的值。 |
P
PaDoBoo 已提交
269

G
ge-yafang 已提交
270
**示例:**
271 272 273 274
```js
let promise = preferences.get('startup', 'default');
promise.then((data) => {
    console.info("Succeeded in getting value of 'startup'. Data: " + data);
P
PaDaBoo 已提交
275
}).catch((err) => {
276
    console.info("Failed to get value of 'startup'. Cause: " + err);
P
PaDaBoo 已提交
277 278
})
```
P
PaDoBoo 已提交
279

280 281 282 283
### getAll

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

284
获取含有所有键值的Object对象。
285 286 287 288

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**
289 290 291
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;Object&gt; | 是   | 回调函数。当获取成功,err为undefined,value为含有所有键值的Object对象;否则err为错误码。 |
292 293

**示例:**
294 295

```js
L
li_juntao 已提交
296
preferences.getAll(function (err, value) {
297
    if (err) {
298 299
        console.info("Failed to get all key-values. Cause: " + err);
        return;
300
    }
301 302
    let allKeys = Object.keys(value);
    console.info("getAll keys = " + allKeys);
303
    console.info("getAll object = " + JSON.stringify(value));
304 305 306 307 308 309 310 311
});
```


### getAll

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

312
获取含有所有键值的Object对象。
313 314 315 316

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**返回值:**
317 318 319
| 类型                  | 说明                                        |
| --------------------- | ------------------------------------------- |
| Promise&lt;Object&gt; | Promise对象,返回含有所有键值的Object对象。 |
320 321

**示例:**
322 323
```js
let promise = preferences.getAll();
324
promise.then((value) => {
325 326
    let allKeys = Object.keys(value);
    console.info('getAll keys = ' + allKeys);
327
    console.info("getAll object = " + JSON.stringify(value));
328
}).catch((err) => {
329
    console.info("Failed to get all key-values. Cause: " + err);
330 331
})
```
P
PaDoBoo 已提交
332 333 334 335 336

### put

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

337
将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。
P
PaDoBoo 已提交
338

P
PaDaBoo 已提交
339
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
340

G
ge-yafang 已提交
341
**参数:**
342 343 344 345 346
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                    | 是   | 要修改的存储的Key,不能为空。                                |
| value    | [ValueType](#valuetype)   | 是   | 存储的新值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当数据写入成功,err为undefined;否则为错误码。     |
P
PaDoBoo 已提交
347

G
ge-yafang 已提交
348
**示例:**
349
```js
350
preferences.put('startup', 'auto', function (err) {
P
PaDaBoo 已提交
351
    if (err) {
352 353
        console.info("Failed to put value of 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
354
    }
355
    console.info("Succeeded in putting value of 'startup'.");
P
PaDaBoo 已提交
356 357
})
```
P
PaDoBoo 已提交
358 359 360 361 362 363


### put

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

364
将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。
P
PaDoBoo 已提交
365

P
PaDaBoo 已提交
366
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
367

G
ge-yafang 已提交
368
**参数:**
369 370 371 372
| 参数名 | 类型                    | 必填 | 说明                                                         |
| ------ | ----------------------- | ---- | ------------------------------------------------------------ |
| key    | string                  | 是   | 要修改的存储的Key,不能为空。                                |
| value  | [ValueType](#valuetype) | 是   | 存储的新值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
P
PaDoBoo 已提交
373

G
ge-yafang 已提交
374
**返回值:**
375 376 377
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
378

G
ge-yafang 已提交
379
**示例:**
380 381
```js
let promise = preferences.put('startup', 'auto');
P
PaDaBoo 已提交
382
promise.then(() => {
383
    console.info("Succeeded in putting value of 'startup'.");
P
PaDaBoo 已提交
384
}).catch((err) => {
385
    console.info("Failed to put value of 'startup'. Cause: " + err);
P
PaDaBoo 已提交
386 387
})
```
P
PaDoBoo 已提交
388 389 390 391


### has

392
has(key: string, callback: AsyncCallback&lt;boolean&gt;): void
P
PaDoBoo 已提交
393

394
检查存储对象是否包含名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
395

P
PaDaBoo 已提交
396
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
397

G
ge-yafang 已提交
398
**参数:**
399 400 401 402
| 参数名   | 类型                         | 必填 | 说明                                                         |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                       | 是   | 要检查的存储key名称,不能为空。                              |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
403

G
ge-yafang 已提交
404
**示例:**
405
```js
406
preferences.has('startup', function (err, isExist) {
P
PaDaBoo 已提交
407
    if (err) {
408
        console.info("Failed to check the key 'startup'. Cause: " + err);
409
        return;
P
PaDaBoo 已提交
410 411
    }
    if (isExist) {
412
        console.info("The key 'startup' is contained.");
P
PaDaBoo 已提交
413
    } else {
414
        console.info("The key 'startup' dose not contain.");
P
PaDaBoo 已提交
415 416 417
    }
})
```
P
PaDoBoo 已提交
418 419 420 421 422 423


### has

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

424
检查存储对象是否包含名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
425

P
PaDaBoo 已提交
426
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
427

G
ge-yafang 已提交
428
**参数:**
429 430 431
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
432

G
ge-yafang 已提交
433
**返回值:**
434 435 436
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
| Promise&lt;boolean&gt; | Promise对象。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
437

G
ge-yafang 已提交
438
**示例:**
439 440 441

```js
let promise = preferences.has('startup');
P
PaDaBoo 已提交
442 443
promise.then((isExist) => {
    if (isExist) {
444
        console.info("The key 'startup' is contained.");
P
PaDaBoo 已提交
445
    } else {
446
        console.info("The key 'startup' dose not contain.");
P
PaDaBoo 已提交
447 448
    }
}).catch((err) => {
449
    console.info("Failed to check the key 'startup'. Cause: " + err);
P
PaDaBoo 已提交
450 451
})
```
P
PaDoBoo 已提交
452 453 454 455 456 457


### delete

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

458
从存储对象中删除名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
459

P
PaDaBoo 已提交
460
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
461

G
ge-yafang 已提交
462
**参数:**
463 464 465 466
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| key      | string                    | 是   | 要删除的存储Key名称,不能为空。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当删除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
467

G
ge-yafang 已提交
468
**示例:**
469
```js
470
preferences.delete('startup', function (err) {
P
PaDaBoo 已提交
471
    if (err) {
472 473
        console.info("Failed to delete the key 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
474
    }
475
    console.info("Succeeded in deleting the key 'startup'.");
P
PaDaBoo 已提交
476 477
})
```
P
PaDoBoo 已提交
478 479 480 481 482 483


### delete

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

484
从存储对象删除名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
485

P
PaDaBoo 已提交
486
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
487

G
ge-yafang 已提交
488
**参数:**
489 490 491
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
492

G
ge-yafang 已提交
493
**返回值:**
494 495 496
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
497

G
ge-yafang 已提交
498
**示例:**
499 500
```js
let promise = preferences.delete('startup');
P
PaDaBoo 已提交
501
promise.then(() => {
502
    console.info("Succeeded in deleting the key 'startup'.");
P
PaDaBoo 已提交
503
}).catch((err) => {
504
    console.info("Failed to delete the key 'startup'. Cause: " + err);
P
PaDaBoo 已提交
505 506
})
```
P
PaDoBoo 已提交
507 508 509 510 511 512


### flush

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

513
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,使用callback异步回调。
P
PaDoBoo 已提交
514

P
PaDaBoo 已提交
515
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
516

G
ge-yafang 已提交
517
**参数:**
518 519 520
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当保存成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
521

G
ge-yafang 已提交
522
**示例:**
523
```js
524
preferences.flush(function (err) {
P
PaDaBoo 已提交
525
    if (err) {
526 527
        console.info("Failed to flush. Cause: " + err);
        return;
P
PaDaBoo 已提交
528
    }
529
    console.info("Succeeded in flushing.");
P
PaDaBoo 已提交
530 531
})
```
P
PaDoBoo 已提交
532 533 534 535 536 537


### flush

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

538
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,使用Promise异步回调。
P
PaDoBoo 已提交
539

P
PaDaBoo 已提交
540
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
541

G
ge-yafang 已提交
542
**返回值:**
543 544 545
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
546

G
ge-yafang 已提交
547
**示例:**
548 549
```js
let promise = preferences.flush();
P
PaDaBoo 已提交
550
promise.then(() => {
551
    console.info("Succeeded in flushing.");
P
PaDaBoo 已提交
552
}).catch((err) => {
553
    console.info("Failed to flush. Cause: " + err);
P
PaDaBoo 已提交
554 555
})
```
P
PaDoBoo 已提交
556 557 558 559 560 561


### clear

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

562
清除此存储对象中的所有存储,使用callback异步回调。
P
PaDoBoo 已提交
563

P
PaDaBoo 已提交
564
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
565

G
ge-yafang 已提交
566
**参数:**
567 568 569
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当清除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
570

G
ge-yafang 已提交
571
**示例:**
572
```js
573
preferences.clear(function (err) {
P
PaDaBoo 已提交
574
    if (err) {
575 576
        console.info("Failed to clear. Cause: " + err);
        return;
P
PaDaBoo 已提交
577
    }
578
    console.info("Succeeded in clearing.");
P
PaDaBoo 已提交
579 580
})
```
P
PaDoBoo 已提交
581 582 583 584 585 586


### clear

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

587
清除此存储对象中的所有存储,使用Promise异步回调。
P
PaDoBoo 已提交
588

P
PaDaBoo 已提交
589
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
590

G
ge-yafang 已提交
591
**返回值:**
592 593 594
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
595

G
ge-yafang 已提交
596
**示例:**
597
```js
598
let promise = preferences.clear()
P
PaDaBoo 已提交
599
promise.then(() => {
600
    console.info("Succeeded in clearing.");
P
PaDaBoo 已提交
601
}).catch((err) => {
602
    console.info("Failed to clear. Cause: " + err);
P
PaDaBoo 已提交
603 604
})
```
P
PaDoBoo 已提交
605 606 607 608 609 610


### on('change')

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

611
订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。
P
PaDoBoo 已提交
612

P
PaDaBoo 已提交
613
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
614

G
ge-yafang 已提交
615
**参数:**
616 617 618 619
| 参数名   | 类型                             | 必填 | 说明                                     |
| -------- | -------------------------------- | ---- | ---------------------------------------- |
| type     | string                           | 是   | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;{ key : string }&gt; | 是   | 回调对象实例。                           |
P
PaDoBoo 已提交
620

G
ge-yafang 已提交
621
**示例:**
622
```js
L
li_juntao 已提交
623
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDaBoo 已提交
624
    if (err) {
625
        console.info("Failed to Gget preferences.");
L
li_juntao 已提交
626
        return;
P
PaDaBoo 已提交
627
    }
L
li_juntao 已提交
628
    var observer = function (key) {
629
        console.info("The key " + key + " changed.");
L
li_juntao 已提交
630
    }
631
    preferences.on('change', observer);
L
li_juntao 已提交
632
    preferences.put('startup', 'auto', function (err) {
P
PaDaBoo 已提交
633
        if (err) {
634 635
            console.info("Failed to put the value of 'startup'. Cause: " + err);
            return;
P
PaDaBoo 已提交
636
        }
637
        console.info("Succeeded in putting the value of 'startup'.");
L
li_juntao 已提交
638 639 640

        preferences.flush(function (err) {
            if (err) {
641 642
                console.info("Failed to flush. Cause: " + err);
                return;
L
li_juntao 已提交
643
            }
644
            console.info("Succeeded in flushing."); // observer will be called.
L
li_juntao 已提交
645
        })
P
PaDaBoo 已提交
646 647 648
    })
})
```
P
PaDoBoo 已提交
649 650 651 652


### off('change')

653
off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void
P
PaDoBoo 已提交
654

655
取消订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。
P
PaDoBoo 已提交
656

P
PaDaBoo 已提交
657
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
658

G
ge-yafang 已提交
659
**参数:**
660 661 662 663
| 参数名   | 类型                             | 必填 | 说明                                       |
| -------- | -------------------------------- | ---- | ------------------------------------------ |
| type     | string                           | 是   | 事件类型,固定值'change',表示数据变更。   |
| callback | Callback&lt;{ key : string }&gt; | 否   | 需要取消的回调对象实例,不填写则全部取消。 |
P
PaDoBoo 已提交
664

G
ge-yafang 已提交
665
**示例:**
666
```js
L
li_juntao 已提交
667
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDaBoo 已提交
668
    if (err) {
669
        console.info("Failed to Gget preferences.");
L
li_juntao 已提交
670
        return;
P
PaDaBoo 已提交
671
    }
L
li_juntao 已提交
672
    var observer = function (key) {
673
        console.info("The key " + key + " changed.");
L
li_juntao 已提交
674
    }
675
    preferences.on('change', observer);
L
li_juntao 已提交
676
    preferences.put('startup', 'auto', function (err) {
P
PaDaBoo 已提交
677
        if (err) {
678 679
            console.info("Failed to put the value of 'startup'. Cause: " + err);
            return;
P
PaDaBoo 已提交
680
        }
681
        console.info("Succeeded in putting the value of 'startup'.");
L
li_juntao 已提交
682 683 684

        preferences.flush(function (err) {
            if (err) {
685 686
                console.info("Failed to flush. Cause: " + err);
                return;
L
li_juntao 已提交
687
            }
688
            console.info("Succeeded in flushing."); // observer will be called.
L
li_juntao 已提交
689
        })
690
        preferences.off('change', observer);
P
PaDaBoo 已提交
691 692 693
    })
})
```
G
ge-yafang 已提交
694 695 696 697 698

## ValueType

用于表示允许的数据字段类型。

P
PaDaBoo 已提交
699
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
G
ge-yafang 已提交
700

701 702 703 704 705
| 类型            | 说明                           |
| --------------- | ------------------------------ |
| number          | 表示值类型为数字。             |
| string          | 表示值类型为字符串。           |
| boolean         | 表示值类型为布尔值。           |
706 707
| Array\<number>  | 表示值类型为数字类型的数组。   |
| Array\<boolean> | 表示值类型为布尔类型的数组。   |
708
| Array\<string>  | 表示值类型为字符串类型的数组。 |