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

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


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
7
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
P
PaDoBoo 已提交
8 9 10 11


## 导入模块

W
wangxiyue 已提交
12
```ts
P
PaDoBoo 已提交
13
import data_preferences from '@ohos.data.preferences'
P
PaDoBoo 已提交
14 15 16 17
```

## 属性

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

P
PaDoBoo 已提交
20 21
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
22 23
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 |
| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 |
W
wangxiyue 已提交
24
| ValueType | number丨string丨boolean | 是 | 否 | 默认返回值,支持number、string、boolean。 |
P
PaDoBoo 已提交
25 26


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

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

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

P
PaDaBoo 已提交
33

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

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

- 示例:
W
wangxiyue 已提交
44
  ```ts
P
PaDoBoo 已提交
45
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
46
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
47

P
PaDoBoo 已提交
48
  data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
49
      if (err) {
W
wangxiyue 已提交
50
          console.info("Get the preferences failed")
P
PaDoBoo 已提交
51 52
          return;
      }
P
PaDoBoo 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
      preferences.put('startup', 'auto', function (err) {
          if (err) {
              console.info("Put the value of startup failed with err: " + err)
              return
          }
          console.info("Put the value of startup successfully.")
          preferences.flush(function (err) {
              if (err) {
                  console.info("Flush to file failed with err: " + err)
                  return
              }
              console.info("Flushed to file successfully.")
          })
      })
P
PaDoBoo 已提交
67 68 69 70
  })
  ```


P
PaDoBoo 已提交
71
## data_preferences.getPreferences
P
PaDoBoo 已提交
72

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

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

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

P
PaDoBoo 已提交
79 80 81
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
82
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDoBoo 已提交
83 84 85 86 87 88 89 90
  | name | string | 是 | 应用程序内部数据存储名称。 |

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
91
  ```ts
P
PaDaBoo 已提交
92
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
93
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
94 95 96

  let promise = data_preferences.getPreferences(this.context, 'mystore')
  promise.then((preferences) => {
P
PaDoBoo 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
      preferences.put('startup', 'auto', function (err) {
          if (err) {
              console.info("Put the value of startup failed with err: " + err)
              return
          }
          console.info("Put the value of startup successfully.")
          preferences.flush(function (err) {
              if (err) {
                  console.info("Flush to file failed with err: " + err)
                  return
              }
              console.info("Flushed to file successfully.")
          })
      })
P
PaDoBoo 已提交
111
  }).catch((err) => {
W
wangxiyue 已提交
112
      console.info("Get the preferences failed")
P
PaDoBoo 已提交
113 114 115 116
  })
  ```


P
PaDoBoo 已提交
117
## data_preferences.deletePreferences
P
PaDoBoo 已提交
118

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

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

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

P
PaDoBoo 已提交
126 127 128
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
129
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
130
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
131 132 133
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
134
  ```ts
P
PaDaBoo 已提交
135
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
136
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
137

P
PaDoBoo 已提交
138
  data_preferences.deletePreferences(this.context, 'mystore', function (err) {
P
PaDoBoo 已提交
139 140 141 142 143 144 145 146 147
      if (err) {
          console.info("Deleted failed with err: " + err)
          return
      }
      console.info("Deleted successfully.")
  })
  ```


P
PaDoBoo 已提交
148
## data_preferences.deletePreferences
P
PaDoBoo 已提交
149

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

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

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

P
PaDoBoo 已提交
157 158 159
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
160
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
161
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
162 163 164 165 166 167 168

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
169
  ```ts
P
PaDaBoo 已提交
170
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
171
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
172 173 174

  let proDelete = data_preferences.deletePreferences(this.context, 'mystore')
  proDelete.then(() => {
P
PaDoBoo 已提交
175 176 177 178 179 180 181
      console.info("Deleted successfully.")
  }).catch((err) => {
      console.info("Deleted failed with err: " + err)
  })
  ```


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

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

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

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

P
PaDoBoo 已提交
190 191 192
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
193
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
194
  | name | string | 是 | 应用程序内部数据存储名称。 |
195
  | callback | AsyncCallback<void> | 是 | 回调函数。 |
P
PaDoBoo 已提交
196 197

- 示例:
W
wangxiyue 已提交
198
  ```ts
P
PaDaBoo 已提交
199
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
200
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
201

P
PaDoBoo 已提交
202
  data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
P
PaDoBoo 已提交
203 204 205 206 207 208 209 210 211
      if (err) {
          console.info("Removed preferences from cache failed with err: " + err)
          return
      }
      console.info("Removed preferences from cache successfully.")
  })
  ```


P
PaDoBoo 已提交
212
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
213

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

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

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

P
PaDoBoo 已提交
220 221 222
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
223
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
224
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
225 226 227 228 229 230 231

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
232
  ```ts
P
PaDaBoo 已提交
233
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
234
  import data_preferences from '@ohos.data.preferences'
W
wangxiyue 已提交
235 236 237

  let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
  promise.then(() => {
P
PaDoBoo 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      console.info("Removed preferences from cache successfully.")
  }).catch((err) => {
      console.info("Removed preferences from cache failed with err: " + err)
  })
  ```


## Preferences

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


### get

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

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

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

P
PaDoBoo 已提交
258 259 260
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
261 262
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
  | defValue | [ValueType](#属性) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
263 264 265
  | callback | AsyncCallback<ValueType> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
266
  ```ts
P
PaDoBoo 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280
  preferences.get('startup', 'default', function(err, value) {
      if (err) {
          console.info("Get the value of startup failed with err: " + err)
          return
      }
      console.info("The value of startup is " + value)
  })
  ```


### get

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

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

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

P
PaDoBoo 已提交
285 286 287
- **参数:**
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
288 289
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
  | defValue | [ValueType](#属性) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
290 291 292 293 294 295 296

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<ValueType> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
297
  ```ts
298 299
  let promiseget = preferences.get('startup', 'default')
  promiseget.then((value) => {
P
PaDoBoo 已提交
300 301 302 303 304 305 306 307 308 309 310
      console.info("The value of startup is " + value)
  }).catch((err) => {
      console.info("Get the value of startup failed with err: " + err)
  })
  ```


### put

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

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

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

P
PaDoBoo 已提交
315 316 317
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
318 319
  | key | string | 是 | 要修改的存储的key,不能为空。 |
  | value | [ValueType](#属性) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
320 321 322
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
323
  ```ts
P
PaDoBoo 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337
  preferences.put('startup', 'auto', function (err) {
      if (err) {
          console.info("Put the value of startup failed with err: " + err)
          return
      }
      console.info("Put the value of startup successfully.")
  })
  ```


### put

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

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

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

P
PaDoBoo 已提交
342 343 344
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
345 346
  | key | string | 是 | 要修改的存储的key,不能为空。 |
  | value | [ValueType](#属性) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
347 348 349 350 351 352 353

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
354
  ```ts
355 356
  let promiseput = preferences.put('startup', 'auto')
  promiseput.then(() => {
P
PaDoBoo 已提交
357 358 359 360 361 362 363 364 365 366 367
      console.info("Put the value of startup successfully.")
  }).catch((err) => {
      console.info("Put the value of startup failed with err: " + err)
  })
  ```


### has

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

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

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

P
PaDoBoo 已提交
372 373 374
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
375
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
376 377 378 379 380 381 382 383
  | callback | AsyncCallback<boolean> | 是 | 回调函数。 |

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | boolean | true表示存在,false表示不存在。 |

- 示例:
W
wangxiyue 已提交
384
  ```ts
P
PaDoBoo 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  preferences.has('startup', function (err, isExist) {
      if (err) {
          console.info("Check the key of startup failed with err: " + err)
          return
      }
      if (isExist) {
          console.info("The key of startup is contained.")
      }
  })
  ```


### has

has(key: string): Promise<boolean>

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

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

P
PaDoBoo 已提交
405 406 407
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
408
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
409 410 411 412 413 414 415

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<boolean> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
416
  ```ts
417 418
  let promisehas = preferences.has('startup')
  promisehas.then((isExist) => {
P
PaDoBoo 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431
      if (isExist) {
          console.info("The key of startup is contained.")
      }
  }).catch((err) => {
      console.info("Check the key of startup failed with err: " + err)
  })
  ```


### delete

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

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

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

P
PaDoBoo 已提交
436 437 438
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
439
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
440 441 442
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
443
  ```ts
P
PaDoBoo 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457
  preferences.delete('startup', function (err) {
      if (err) {
          console.info("Delete startup key failed with err: " + err)
          return
      }
      console.info("Deleted startup key successfully.")
  })
  ```


### delete

delete(key: string): Promise<void>

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

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

P
PaDoBoo 已提交
462 463 464
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
465
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
466 467 468 469 470 471 472

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
473
  ```ts
474 475
  let promisedel = preferences.delete('startup')
  promisedel.then(() => {
P
PaDoBoo 已提交
476 477 478 479 480 481 482 483 484 485 486
      console.info("Deleted startup key successfully.")
  }).catch((err) => {
      console.info("Delete startup key failed with err: " + err)
  })
  ```


### flush

flush(callback: AsyncCallback<void>): void

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

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

P
PaDoBoo 已提交
491 492 493 494 495 496
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
497
  ```ts
P
PaDoBoo 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511
  preferences.flush(function (err) {
      if (err) {
          console.info("Flush to file failed with err: " + err)
          return
      }
      console.info("Flushed to file successfully.")
  })
  ```


### flush

flush(): Promise<void>

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

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

P
PaDoBoo 已提交
516 517 518 519 520 521
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
522
  ```ts
523 524
  let promiseflush = preferences.flush()
  promiseflush.then(() => {
P
PaDoBoo 已提交
525 526 527 528 529 530 531 532 533 534 535
      console.info("Flushed to file successfully.")
  }).catch((err) => {
      console.info("Flush to file failed with err: " + err)
  })
  ```


### clear

clear(callback: AsyncCallback<void>): void

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

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

P
PaDoBoo 已提交
540 541 542 543 544 545
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
546
  ```ts
P
PaDoBoo 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560
  preferences.clear(function (err) {
      if (err) {
          console.info("Clear to file failed with err: " + err)
          return
      }
      console.info("Cleared to file successfully.")
  })
  ```


### clear

clear(): Promise<void>

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

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

P
PaDoBoo 已提交
565 566 567 568 569 570
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
571
  ```ts
572 573
  let promiseclear = preferences.clear()
  promiseclear.then(() => {
P
PaDoBoo 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586
      console.info("Cleared to file successfully.")
  }).catch((err) => {
      console.info("Clear to file failed with err: " + err)
  })
  ```


### on('change')

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

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

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

P
PaDoBoo 已提交
589 590 591 592 593 594 595
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 回调对象实例。 |

- 示例:
W
wangxiyue 已提交
596
  ```ts
P
PaDoBoo 已提交
597 598 599 600
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  preferences.on('change', observer)
P
PaDoBoo 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613
  preferences.put('startup', 'auto', function (err) {
      if (err) {
          console.info("Put the value of startup failed with err: " + err)
          return
      }
      console.info("Put the value of startup successfully.")
      preferences.flush(function (err) {
          if (err) {
              console.info("Flush to file failed with err: " + err)
              return
          }
          console.info("Flushed to file successfully.")    // observer will be called.
      })
W
wangxiyue 已提交
614
  })
P
PaDoBoo 已提交
615 616 617 618 619 620 621 622 623
  ```


### off('change')

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

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

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

P
PaDoBoo 已提交
626 627 628 629 630 631 632
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 需要取消的回调对象实例。 |

- 示例:
W
wangxiyue 已提交
633
  ```ts
P
PaDoBoo 已提交
634 635 636 637 638
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  preferences.off('change', observer)
  ```