js-apis-data-preferences.md 19.5 KB
Newer Older
W
wangxiyue 已提交
1
# 首选项
P
PaDoBoo 已提交
2

W
wangxiyue 已提交
3
首选项为应用提供key-value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。
P
PaDoBoo 已提交
4 5


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


## 导入模块

W
wangxiyue 已提交
12
```ts
G
ge-yafang 已提交
13
import data_preferences from '@ohos.data.preferences';
P
PaDoBoo 已提交
14 15
```

G
ge-yafang 已提交
16
## 常量
P
PaDoBoo 已提交
17

P
PaDaBoo 已提交
18
**系统能力:**以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core
19

P
PaDoBoo 已提交
20 21
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
22
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 |
G
ge-yafang 已提交
23
| MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,大小为8192字节。 |
P
PaDoBoo 已提交
24 25


P
PaDoBoo 已提交
26
## data_preferences.getPreferences
P
PaDoBoo 已提交
27

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

W
wangxiyue 已提交
30
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
31

P
PaDaBoo 已提交
32

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

G
ge-yafang 已提交
35
**参数:**
P
PaDoBoo 已提交
36 37
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
38
  | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDoBoo 已提交
39 40 41
  | name | string | 是 | 应用程序内部数据存储名称。 |
  | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 |

G
ge-yafang 已提交
42
**示例:**
P
PaDaBoo 已提交
43 44 45 46 47 48 49 50 51
```ts
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
    if (err) {
        console.info("Get preferences failed.")
        return;
    }
    console.info("Get preferences successfully.")
})
```
P
PaDoBoo 已提交
52 53


P
PaDoBoo 已提交
54
## data_preferences.getPreferences
P
PaDoBoo 已提交
55

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

W
wangxiyue 已提交
58
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
59

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

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

G
ge-yafang 已提交
68
**返回值:**
P
PaDoBoo 已提交
69 70 71 72
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
73
**示例:**
P
PaDaBoo 已提交
74 75 76 77 78 79 80 81
```ts
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
    console.info("Get preferences successfully.")
}).catch((err) => {
    console.info("Get preferences failed.")
})
```
P
PaDoBoo 已提交
82 83


P
PaDoBoo 已提交
84
## data_preferences.deletePreferences
P
PaDoBoo 已提交
85

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

W
wangxiyue 已提交
88 89
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。
删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
90

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

G
ge-yafang 已提交
93
**参数:**
P
PaDoBoo 已提交
94 95
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
96
  | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
97
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
98 99
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
100
**示例:**
P
PaDaBoo 已提交
101 102 103 104 105 106 107 108 109
```ts
data_preferences.deletePreferences(this.context, 'mystore', function (err) {
    if (err) {
        console.info("Deleted preferences failed, err: " + err)
        return
    }
    console.info("Deleted preferences successfully.")
})
```
P
PaDoBoo 已提交
110 111


P
PaDoBoo 已提交
112
## data_preferences.deletePreferences
P
PaDoBoo 已提交
113

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

W
wangxiyue 已提交
116 117
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。
删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
118

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

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

G
ge-yafang 已提交
127
**返回值:**
P
PaDoBoo 已提交
128 129 130 131
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
132
**示例:**
P
PaDaBoo 已提交
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("Deleted preferences failed, err: " + err)
})
```
P
PaDoBoo 已提交
141 142


P
PaDoBoo 已提交
143
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
144

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

G
ge-yafang 已提交
147 148 149
从内存中移除指定首选项持久化文件对应的Preferences单实例。

移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
150

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

G
ge-yafang 已提交
153
**参数:**
P
PaDoBoo 已提交
154 155
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
156
  | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
157
  | name | string | 是 | 应用程序内部数据存储名称。 |
158
  | callback | AsyncCallback<void> | 是 | 回调函数。 |
P
PaDoBoo 已提交
159

G
ge-yafang 已提交
160
**示例:**
P
PaDaBoo 已提交
161 162 163 164 165 166 167 168 169
```ts
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
    if (err) {
        console.info("Removed preferences from cache failed, err: " + err)
        return
    }
    console.info("Removed preferences from cache successfully.")
})
```
P
PaDoBoo 已提交
170 171


P
PaDoBoo 已提交
172
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
173

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

G
ge-yafang 已提交
176 177 178
从内存中移除指定首选项持久化文件对应的Preferences单实例。

移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
179

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

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

G
ge-yafang 已提交
188
**返回值:**
P
PaDoBoo 已提交
189 190 191 192
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
193
**示例:**
P
PaDaBoo 已提交
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("Removed preferences from cache failed, err: " + err)
})
```
P
PaDoBoo 已提交
202 203 204 205 206 207 208 209 210 211 212


## Preferences

提供获取和修改存储数据的接口。


### get

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

W
wangxiyue 已提交
213
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
214

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

G
ge-yafang 已提交
217
**参数:**
P
PaDoBoo 已提交
218 219
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
220
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
G
ge-yafang 已提交
221
  | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
222 223
  | callback | AsyncCallback<ValueType> | 是 | 回调函数。 |

G
ge-yafang 已提交
224
**示例:**
P
PaDaBoo 已提交
225 226 227 228 229 230 231 232 233
```ts
 preferences.get('startup', 'default', function(err, value) {
    if (err) {
        console.info("Get value of startup failed, err: " + err)
        return
    }
    console.info("Get value of startup is " + value)
})
```
P
PaDoBoo 已提交
234 235 236 237 238 239


### get

get(key: string, defValue: ValueType): Promise<ValueType>

W
wangxiyue 已提交
240
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
241

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

P
PaDoBoo 已提交
244 245 246
- **参数:**
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
247
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
G
ge-yafang 已提交
248
  | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
249

G
ge-yafang 已提交
250
**返回值:**
P
PaDoBoo 已提交
251 252 253 254
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<ValueType> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
255
**示例:**
P
PaDaBoo 已提交
256 257 258 259 260 261 262 263
```ts
let promise = preferences.get('startup', 'default')
promise.then((value) => {
    console.info("Get value of startup is " + value)
}).catch((err) => {
    console.info("Get value of startup failed, err: " + err)
})
```
P
PaDoBoo 已提交
264 265 266 267 268 269


### put

put(key: string, value: ValueType, callback: AsyncCallback<void>): void

W
wangxiyue 已提交
270
首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
271

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

G
ge-yafang 已提交
274
**参数:**
P
PaDoBoo 已提交
275 276
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
277
  | key | string | 是 | 要修改的存储的key,不能为空。 |
G
ge-yafang 已提交
278
  | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
279 280
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
281
**示例:**
P
PaDaBoo 已提交
282 283 284 285 286 287 288 289 290
```ts
preferences.put('startup', 'auto', function (err) {
    if (err) {
        console.info("Put value of startup failed, err: " + err)
        return
    }
    console.info("Put value of startup successfully.")
})
```
P
PaDoBoo 已提交
291 292 293 294 295 296


### put

put(key: string, value: ValueType): Promise<void>

W
wangxiyue 已提交
297
首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
298

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

G
ge-yafang 已提交
301
**参数:**
P
PaDoBoo 已提交
302 303
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
304
  | key | string | 是 | 要修改的存储的key,不能为空。 |
G
ge-yafang 已提交
305
  | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
306

G
ge-yafang 已提交
307
**返回值:**
P
PaDoBoo 已提交
308 309 310 311
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
312
**示例:**
P
PaDaBoo 已提交
313 314 315 316 317 318 319 320
```ts
let promise = preferences.put('startup', 'auto')
promise.then(() => {
    console.info("Put value of startup successfully.")
}).catch((err) => {
    console.info("Put value of startup failed, err: " + err)
})
```
P
PaDoBoo 已提交
321 322 323 324 325 326


### has

has(key: string, callback: AsyncCallback<boolean>): boolean

W
wangxiyue 已提交
327
检查存储对象是否包含名为给定key的存储键值对,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
328

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

G
ge-yafang 已提交
331
**参数:**
P
PaDoBoo 已提交
332 333
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
334
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
335 336
  | callback | AsyncCallback<boolean> | 是 | 回调函数。 |

G
ge-yafang 已提交
337
**返回值:**
P
PaDoBoo 已提交
338 339 340 341
  | 类型 | 说明 |
  | -------- | -------- |
  | boolean | true表示存在,false表示不存在。 |

G
ge-yafang 已提交
342
**示例:**
P
PaDaBoo 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355
```ts
preferences.has('startup', function (err, isExist) {
    if (err) {
        console.info("Check the key of startup failed, err: " + err)
        return
    }
    if (isExist) {
        console.info("The key of startup is contained.")
    } else {
        console.info("The key of startup dose not contain.")
    }
})
```
P
PaDoBoo 已提交
356 357 358 359 360 361


### has

has(key: string): Promise<boolean>

W
wangxiyue 已提交
362
检查存储对象是否包含名为给定key的存储键值对,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
363

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

G
ge-yafang 已提交
366
**参数:**
P
PaDoBoo 已提交
367 368
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
369
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
370

G
ge-yafang 已提交
371
**返回值:**
P
PaDoBoo 已提交
372 373 374 375
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<boolean> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
376
**示例:**
P
PaDaBoo 已提交
377 378 379 380 381 382 383 384 385 386 387 388
```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 dose not contain.")
    }
}).catch((err) => {
    console.info("Check the key of startup failed, err: " + err)
})
```
P
PaDoBoo 已提交
389 390 391 392 393 394


### delete

delete(key: string, callback: AsyncCallback<void>): void

W
wangxiyue 已提交
395
从存储对象中删除名为给定key的存储键值对,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
396

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

G
ge-yafang 已提交
399
**参数:**
P
PaDoBoo 已提交
400 401
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
402
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
403 404
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
405
**示例:**
P
PaDaBoo 已提交
406 407 408 409 410 411 412 413 414
```ts
preferences.delete('startup', function (err) {
    if (err) {
        console.info("Delete startup key failed, err: " + err)
        return
    }
    console.info("Deleted startup key successfully.")
})
```
P
PaDoBoo 已提交
415 416 417 418 419 420


### delete

delete(key: string): Promise<void>

W
wangxiyue 已提交
421
从存储对象删除名为给定key的存储键值对,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
422

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

G
ge-yafang 已提交
425
**参数:**
P
PaDoBoo 已提交
426 427
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
428
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
429

G
ge-yafang 已提交
430
**返回值:**
P
PaDoBoo 已提交
431 432 433 434
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
435
**示例:**
P
PaDaBoo 已提交
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("Delete startup key failed, err: " + err)
})
```
P
PaDoBoo 已提交
444 445 446 447 448 449


### flush

flush(callback: AsyncCallback<void>): void

W
wangxiyue 已提交
450
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
451

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

G
ge-yafang 已提交
454
**参数:**
P
PaDoBoo 已提交
455 456 457 458
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
459
**示例:**
P
PaDaBoo 已提交
460 461 462 463 464 465 466 467 468
```ts
preferences.flush(function (err) {
    if (err) {
        console.info("Flush to file failed, err: " + err)
        return
    }
    console.info("Flushed to file successfully.")
})
```
P
PaDoBoo 已提交
469 470 471 472 473 474


### flush

flush(): Promise<void>

W
wangxiyue 已提交
475
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
476

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

G
ge-yafang 已提交
479
**返回值:**
P
PaDoBoo 已提交
480 481 482 483
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
484
**示例:**
P
PaDaBoo 已提交
485 486 487 488 489 490 491 492
```ts
let promise = preferences.flush()
promise.then(() => {
    console.info("Flushed to file successfully.")
}).catch((err) => {
    console.info("Flush to file failed, err: " + err)
})
```
P
PaDoBoo 已提交
493 494 495 496 497 498


### clear

clear(callback: AsyncCallback<void>): void

W
wangxiyue 已提交
499
清除此存储对象中的所有存储,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
500

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

G
ge-yafang 已提交
503
**参数:**
P
PaDoBoo 已提交
504 505 506 507
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
508
**示例:**
P
PaDaBoo 已提交
509 510 511 512 513 514 515 516 517
```ts
preferences.clear(function (err) {
    if (err) {
        console.info("Clear to file failed, err: " + err)
        return
    }
    console.info("Cleared to file successfully.")
})
```
P
PaDoBoo 已提交
518 519 520 521 522 523


### clear

clear(): Promise<void>

W
wangxiyue 已提交
524
清除此存储对象中的所有存储,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
525

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

G
ge-yafang 已提交
528
**返回值:**
P
PaDoBoo 已提交
529 530 531 532
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
533
**示例:**
P
PaDaBoo 已提交
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("Clear to file failed, err: " + err)
})
```
P
PaDoBoo 已提交
542 543 544 545 546 547 548 549


### on('change')

on(type: 'change', callback: Callback<{ key : string }>): void

订阅数据变更者类,订阅的key的值发生变更后,在执行flush方法后,callback方法会被回调。

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

G
ge-yafang 已提交
552
**参数:**
P
PaDoBoo 已提交
553 554 555 556 557
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 回调对象实例。 |

G
ge-yafang 已提交
558
**示例:**
P
PaDaBoo 已提交
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("Put the value of startup failed, err: " + err)
        return
    }
    console.info("Put the value of startup successfully.")

    preferences.flush(function (err) {
        if (err) {
            console.info("Flush to file failed, err: " + err)
            return
        }
        console.info("Flushed to file successfully.")    // observer will be called.
    })
})
```
P
PaDoBoo 已提交
583 584 585 586 587 588 589 590


### off('change')

off(type: 'change', callback: Callback<{ key : string }>): void

当不再进行订阅数据变更时,使用此接口取消订阅。

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

G
ge-yafang 已提交
593
**参数:**
P
PaDoBoo 已提交
594 595 596 597 598
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 需要取消的回调对象实例。 |

G
ge-yafang 已提交
599
**示例:**
P
PaDaBoo 已提交
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("Put the value of startup failed, err: " + err)
        return
    }
    console.info("Put the value of startup successfully.")

    preferences.flush(function (err) {
        if (err) {
            console.info("Flush to file failed, err: " + err)
            return
        }
        console.info("Flushed to file successfully.")    // observer will be called.
        preferences.off('change', observer)
    })
})
```
G
ge-yafang 已提交
625 626 627 628 629

## ValueType

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

P
PaDaBoo 已提交
630
**系统能力:**SystemCapability.DistributedDataManager.Preferences.Core
G
ge-yafang 已提交
631 632 633 634 635 636

| 名称    | 说明                 |
| ------- | -------------------- |
| number  | 表示值类型为数字。   |
| string  | 表示值类型为字符。   |
| boolean | 表示值类型为布尔值。 |