You need to sign in or sign up before continuing.
js-apis-data-preferences.md 26.1 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

| 参数名   | 类型                                             | 必填 | 说明                                                         |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| context  | [Context](js-apis-ability-context.md)            | 是   | 应用上下文。                                                 |
42 43
| name     | string                                           | 是   | Preferences实例的名称。                                      |
| callback | AsyncCallback<[Preferences](#preferences)> | 是   | 回调函数。当获取Preferences实例成功,err为undefined,返回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 73

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用上下文。            |
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
74

G
ge-yafang 已提交
75
**返回值:**
76

77 78 79
| 类型                                       | 说明                               |
| ------------------------------------------ | ---------------------------------- |
| Promise<[Preferences](#preferences)> | Promise对象,返回Preferences实例。 |
P
PaDoBoo 已提交
80

G
ge-yafang 已提交
81
**示例:**
82 83 84

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


95
## data_preferences.deletePreferences
P
PaDoBoo 已提交
96

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

99 100 101
从内存中移除指定的Preferences实例,使用callback异步回调。

若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
102

103
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
104

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

G
ge-yafang 已提交
107
**参数:**
108

109 110 111
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context  | [Context](js-apis-ability-context.md) | 是   | 应用上下文。                                         |
112
| name     | string                                | 是   | Preferences实例的名称。                              |
113
| callback | AsyncCallback<void>             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
114

G
ge-yafang 已提交
115
**示例:**
116

117
```js
118
data_preferences.deletePreferences(this.context, 'mystore', function (err) {
P
PaDaBoo 已提交
119
    if (err) {
120
        console.info("Failed to delete preferences. Cause: " + err);
P
PaDaBoo 已提交
121 122
        return
    }
123
    console.info("Succeeded in deleting preferences." );
P
PaDaBoo 已提交
124 125
})
```
P
PaDoBoo 已提交
126 127


128
## data_preferences.deletePreferences
P
PaDoBoo 已提交
129

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

132 133 134
从内存中移除指定的Preferences实例,使用Promise异步回调。

若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
135

136
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
137

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

G
ge-yafang 已提交
140
**参数:**
141 142 143 144 145

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用上下文。            |
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
146

G
ge-yafang 已提交
147
**返回值:**
148

149 150 151
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise<void> | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
152

G
ge-yafang 已提交
153
**示例:**
154

155
```js
156
let promise = data_preferences.deletePreferences(this.context, 'mystore')
P
PaDaBoo 已提交
157
promise.then(() => {
158
    console.info("Succeeded in deleting preferences.");
P
PaDaBoo 已提交
159
}).catch((err) => {
160
    console.info("Failed to delete preferences. Cause: " + err);
P
PaDaBoo 已提交
161 162
})
```
P
PaDoBoo 已提交
163 164


165
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
166

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

169
从内存中移除指定的Preferences实例,使用callback异步回调。
G
ge-yafang 已提交
170

171
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
172

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

G
ge-yafang 已提交
175
**参数:**
176

177 178
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
179 180
| context  | [Context](js-apis-ability-context.md) | 是   | 应用上下文。                                         |
| name     | string                                | 是   | Preferences实例的名称。                              |
181
| callback | AsyncCallback<void>             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
182

G
ge-yafang 已提交
183
**示例:**
184

185
```js
186
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
P
PaDaBoo 已提交
187
    if (err) {
188 189
        console.info("Failed to remove preferences. Cause: " + err);
        return;
P
PaDaBoo 已提交
190
    }
191
    console.info("Succeeded in removing preferences.");
P
PaDaBoo 已提交
192 193
})
```
P
PaDoBoo 已提交
194 195


196
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
197

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

200
从内存中移除指定的Preferences实例,使用callback异步回调。
G
ge-yafang 已提交
201

202
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
203

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

G
ge-yafang 已提交
206
**参数:**
207 208 209 210 211

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | 是   | 应用上下文。            |
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
212

G
ge-yafang 已提交
213
**返回值:**
214

215 216 217
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise<void> | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
218

G
ge-yafang 已提交
219
**示例:**
220

221
```js
222
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
P
PaDaBoo 已提交
223
promise.then(() => {
224
    console.info("Succeeded in removing preferences.");
P
PaDaBoo 已提交
225
}).catch((err) => {
226
    console.info("Failed to remove preferences. Cause: " + err);
P
PaDaBoo 已提交
227 228
})
```
P
PaDoBoo 已提交
229 230 231 232


## Preferences

233 234 235
存储实例,提供获取和修改存储数据的接口。

下列接口都需先使用[data_preferences.getPreferences](#data_preferencesgetpreferences)获取到Preferences实例,再通过此实例调用对应接口。
P
PaDoBoo 已提交
236 237 238 239 240 241


### get

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

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

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

G
ge-yafang 已提交
246
**参数:**
247

248 249 250 251 252
| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
253

G
ge-yafang 已提交
254
**示例:**
255 256 257

```js
preferences.get('startup', 'default', function(err, data) {
P
PaDaBoo 已提交
258
    if (err) {
259 260
        console.info("Failed to get value of 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
261
    }
262
    console.info("Succeeded in getting value of 'startup'. Data: " + data);
P
PaDaBoo 已提交
263 264
})
```
P
PaDoBoo 已提交
265 266 267 268 269 270


### get

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

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

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

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

G
ge-yafang 已提交
282
**返回值:**
283 284 285 286

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

G
ge-yafang 已提交
288
**示例:**
289

290 291 292 293
```js
let promise = preferences.get('startup', 'default');
promise.then((data) => {
    console.info("Succeeded in getting value of 'startup'. Data: " + data);
P
PaDaBoo 已提交
294
}).catch((err) => {
295
    console.info("Failed to get value of 'startup'. Cause: " + err);
P
PaDaBoo 已提交
296 297
})
```
P
PaDoBoo 已提交
298

299 300 301 302
### getAll

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

303
获取含有所有键值的Object对象。
304 305 306 307

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

**参数:**
308

309 310 311
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;Object&gt; | 是   | 回调函数。当获取成功,err为undefined,value为含有所有键值的Object对象;否则err为错误码。 |
312 313

**示例:**
314 315

```js
L
li_juntao 已提交
316
preferences.getAll(function (err, value) {
317
    if (err) {
318 319
        console.info("Failed to get all key-values. Cause: " + err);
        return;
320
    }
321 322
    let allKeys = Object.keys(value);
    console.info("getAll keys = " + allKeys);
323
    console.info("getAll object = " + JSON.stringify(value));
324 325 326 327 328 329 330 331
});
```


### getAll

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

332
获取含有所有键值的Object对象。
333 334 335 336

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

**返回值:**
337

338 339 340
| 类型                  | 说明                                        |
| --------------------- | ------------------------------------------- |
| Promise&lt;Object&gt; | Promise对象,返回含有所有键值的Object对象。 |
341 342

**示例:**
343

344 345
```js
let promise = preferences.getAll();
346
promise.then((value) => {
347 348
    let allKeys = Object.keys(value);
    console.info('getAll keys = ' + allKeys);
349
    console.info("getAll object = " + JSON.stringify(value));
350
}).catch((err) => {
351
    console.info("Failed to get all key-values. Cause: " + err);
352 353
})
```
P
PaDoBoo 已提交
354 355 356 357 358

### put

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

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

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

G
ge-yafang 已提交
363
**参数:**
364

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

G
ge-yafang 已提交
371
**示例:**
372

373
```js
374
preferences.put('startup', 'auto', function (err) {
P
PaDaBoo 已提交
375
    if (err) {
376 377
        console.info("Failed to put value of 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
378
    }
379
    console.info("Succeeded in putting value of 'startup'.");
P
PaDaBoo 已提交
380 381
})
```
P
PaDoBoo 已提交
382 383 384 385 386 387


### put

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

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

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

G
ge-yafang 已提交
392
**参数:**
393

394 395 396 397
| 参数名 | 类型                    | 必填 | 说明                                                         |
| ------ | ----------------------- | ---- | ------------------------------------------------------------ |
| key    | string                  | 是   | 要修改的存储的Key,不能为空。                                |
| value  | [ValueType](#valuetype) | 是   | 存储的新值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
P
PaDoBoo 已提交
398

G
ge-yafang 已提交
399
**返回值:**
400

401 402 403
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
404

G
ge-yafang 已提交
405
**示例:**
406

407 408
```js
let promise = preferences.put('startup', 'auto');
P
PaDaBoo 已提交
409
promise.then(() => {
410
    console.info("Succeeded in putting value of 'startup'.");
P
PaDaBoo 已提交
411
}).catch((err) => {
412
    console.info("Failed to put value of 'startup'. Cause: " + err);
P
PaDaBoo 已提交
413 414
})
```
P
PaDoBoo 已提交
415 416 417 418


### has

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

421
检查Preferences实例是否包含名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
422

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

G
ge-yafang 已提交
425
**参数:**
426

427 428 429
| 参数名   | 类型                         | 必填 | 说明                                                         |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                       | 是   | 要检查的存储key名称,不能为空。                              |
430
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
431

G
ge-yafang 已提交
432
**示例:**
433

434
```js
435
preferences.has('startup', function (err, isExist) {
P
PaDaBoo 已提交
436
    if (err) {
437
        console.info("Failed to check the key 'startup'. Cause: " + err);
438
        return;
P
PaDaBoo 已提交
439 440
    }
    if (isExist) {
441
        console.info("The key 'startup' is contained.");
P
PaDaBoo 已提交
442
    } else {
443
        console.info("The key 'startup' dose not contain.");
P
PaDaBoo 已提交
444 445 446
    }
})
```
P
PaDoBoo 已提交
447 448 449 450 451 452


### has

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

453
检查Preferences实例是否包含名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
454

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

G
ge-yafang 已提交
457
**参数:**
458

459 460 461
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
462

G
ge-yafang 已提交
463
**返回值:**
464

465 466
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
467
| Promise&lt;boolean&gt; | Promise对象。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
468

G
ge-yafang 已提交
469
**示例:**
470 471 472

```js
let promise = preferences.has('startup');
P
PaDaBoo 已提交
473 474
promise.then((isExist) => {
    if (isExist) {
475
        console.info("The key 'startup' is contained.");
P
PaDaBoo 已提交
476
    } else {
477
        console.info("The key 'startup' dose not contain.");
P
PaDaBoo 已提交
478 479
    }
}).catch((err) => {
480
    console.info("Failed to check the key 'startup'. Cause: " + err);
P
PaDaBoo 已提交
481 482
})
```
P
PaDoBoo 已提交
483 484 485 486 487 488


### delete

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

489
从Preferences实例中删除名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
490

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

G
ge-yafang 已提交
493
**参数:**
494

495 496 497 498
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| key      | string                    | 是   | 要删除的存储Key名称,不能为空。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当删除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
499

G
ge-yafang 已提交
500
**示例:**
501

502
```js
503
preferences.delete('startup', function (err) {
P
PaDaBoo 已提交
504
    if (err) {
505 506
        console.info("Failed to delete the key 'startup'. Cause: " + err);
        return;
P
PaDaBoo 已提交
507
    }
508
    console.info("Succeeded in deleting the key 'startup'.");
P
PaDaBoo 已提交
509 510
})
```
P
PaDoBoo 已提交
511 512 513 514 515 516


### delete

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

517
从Preferences实例中删除名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
518

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

G
ge-yafang 已提交
521
**参数:**
522

523 524 525
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
526

G
ge-yafang 已提交
527
**返回值:**
528

529 530 531
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
532

G
ge-yafang 已提交
533
**示例:**
534

535 536
```js
let promise = preferences.delete('startup');
P
PaDaBoo 已提交
537
promise.then(() => {
538
    console.info("Succeeded in deleting the key 'startup'.");
P
PaDaBoo 已提交
539
}).catch((err) => {
540
    console.info("Failed to delete the key 'startup'. Cause: " + err);
P
PaDaBoo 已提交
541 542
})
```
P
PaDoBoo 已提交
543 544 545 546 547 548


### flush

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

549
将当前Preferences实例的数据异步存储到首选项持久化文件中,使用callback异步回调。
P
PaDoBoo 已提交
550

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

G
ge-yafang 已提交
553
**参数:**
554

555 556 557
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当保存成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
558

G
ge-yafang 已提交
559
**示例:**
560

561
```js
562
preferences.flush(function (err) {
P
PaDaBoo 已提交
563
    if (err) {
564 565
        console.info("Failed to flush. Cause: " + err);
        return;
P
PaDaBoo 已提交
566
    }
567
    console.info("Succeeded in flushing.");
P
PaDaBoo 已提交
568 569
})
```
P
PaDoBoo 已提交
570 571 572 573 574 575


### flush

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

576
将当前Preferences实例的数据异步存储到首选项持久化文件中,使用Promise异步回调。
P
PaDoBoo 已提交
577

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

G
ge-yafang 已提交
580
**返回值:**
581

582 583 584
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
585

G
ge-yafang 已提交
586
**示例:**
587

588 589
```js
let promise = preferences.flush();
P
PaDaBoo 已提交
590
promise.then(() => {
591
    console.info("Succeeded in flushing.");
P
PaDaBoo 已提交
592
}).catch((err) => {
593
    console.info("Failed to flush. Cause: " + err);
P
PaDaBoo 已提交
594 595
})
```
P
PaDoBoo 已提交
596 597 598 599 600 601


### clear

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

602
清除此Preferences实例中的所有存储,使用callback异步回调。
P
PaDoBoo 已提交
603

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

G
ge-yafang 已提交
606
**参数:**
607

608 609 610
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当清除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
611

G
ge-yafang 已提交
612
**示例:**
613

614
```js
615
preferences.clear(function (err) {
P
PaDaBoo 已提交
616
    if (err) {
617 618
        console.info("Failed to clear. Cause: " + err);
        return;
P
PaDaBoo 已提交
619
    }
620
    console.info("Succeeded in clearing.");
P
PaDaBoo 已提交
621 622
})
```
P
PaDoBoo 已提交
623 624 625 626 627 628


### clear

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

629
清除此Preferences实例中的所有存储,使用Promise异步回调。
P
PaDoBoo 已提交
630

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

G
ge-yafang 已提交
633
**返回值:**
634

635 636 637
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
638

G
ge-yafang 已提交
639
**示例:**
640

641
```js
642
let promise = preferences.clear()
P
PaDaBoo 已提交
643
promise.then(() => {
644
    console.info("Succeeded in clearing.");
P
PaDaBoo 已提交
645
}).catch((err) => {
646
    console.info("Failed to clear. Cause: " + err);
P
PaDaBoo 已提交
647 648
})
```
P
PaDoBoo 已提交
649 650 651 652 653 654


### on('change')

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

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

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

G
ge-yafang 已提交
659
**参数:**
660

661 662 663 664
| 参数名   | 类型                             | 必填 | 说明                                     |
| -------- | -------------------------------- | ---- | ---------------------------------------- |
| type     | string                           | 是   | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;{ key : string }&gt; | 是   | 回调对象实例。                           |
P
PaDoBoo 已提交
665

G
ge-yafang 已提交
666
**示例:**
667

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

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


### off('change')

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

701
取消订阅数据变更。
P
PaDoBoo 已提交
702

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

G
ge-yafang 已提交
705
**参数:**
706

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

G
ge-yafang 已提交
712
**示例:**
713

714
```js
L
li_juntao 已提交
715
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDaBoo 已提交
716
    if (err) {
717
        console.info("Failed to Gget preferences.");
L
li_juntao 已提交
718
        return;
P
PaDaBoo 已提交
719
    }
L
li_juntao 已提交
720
    var observer = function (key) {
721
        console.info("The key " + key + " changed.");
L
li_juntao 已提交
722
    }
723
    preferences.on('change', observer);
L
li_juntao 已提交
724
    preferences.put('startup', 'auto', function (err) {
P
PaDaBoo 已提交
725
        if (err) {
726 727
            console.info("Failed to put the value of 'startup'. Cause: " + err);
            return;
P
PaDaBoo 已提交
728
        }
729
        console.info("Succeeded in putting the value of 'startup'.");
L
li_juntao 已提交
730 731 732

        preferences.flush(function (err) {
            if (err) {
733 734
                console.info("Failed to flush. Cause: " + err);
                return;
L
li_juntao 已提交
735
            }
736
            console.info("Succeeded in flushing."); // observer will be called.
L
li_juntao 已提交
737
        })
738
        preferences.off('change', observer);
P
PaDaBoo 已提交
739 740 741
    })
})
```
G
ge-yafang 已提交
742 743 744 745 746

## ValueType

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

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

749 750 751 752 753
| 类型            | 说明                           |
| --------------- | ------------------------------ |
| number          | 表示值类型为数字。             |
| string          | 表示值类型为字符串。           |
| boolean         | 表示值类型为布尔值。           |
754 755
| Array\<number>  | 表示值类型为数字类型的数组。   |
| Array\<boolean> | 表示值类型为布尔类型的数组。   |
756
| Array\<string>  | 表示值类型为字符串类型的数组。 |