js-apis-data-preferences.md 18.5 KB
Newer Older
P
PaDoBoo 已提交
1 2 3 4 5 6
# 轻量级存储

轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。


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


## 导入模块

```
P
PaDaBoo 已提交
13
import data_Preferences from '@ohos.data.preferences'
P
PaDoBoo 已提交
14 15 16 17 18 19
```

## 属性

| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
20 21
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。<br>**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core |
| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。<br>**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core |
P
PaDoBoo 已提交
22 23


P
PaDaBoo 已提交
24
## data_Preferences.getPreferences
P
PaDoBoo 已提交
25

P
PaDaBoo 已提交
26
getPreferences(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void
P
PaDoBoo 已提交
27 28 29

读取指定文件,将数据加载到Preferences实例,用于数据操作,使用callback形式返回结果。

P
PaDaBoo 已提交
30

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

P
PaDoBoo 已提交
33 34 35 36 37 38 39 40 41 42
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |
  | callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | 是 | 回调函数。 |

- 示例:
  ```
  import Ability from '@ohos.application.Ability'
P
PaDaBoo 已提交
43
  import data_Preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
44
  var path = this.context.getDataBaseDir()
P
PaDaBoo 已提交
45
  data_Preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
46 47 48 49 50 51 52 53 54 55
      if (err) {
          console.info("Get the preferences failed, path: " + path + '/mystore')
          return;
      }
      preferences.putSync('startup', 'auto')
      preferences.flushSync()
  })
  ```


P
PaDaBoo 已提交
56
## data_Preferences.getPreferences
P
PaDoBoo 已提交
57

P
PaDaBoo 已提交
58
getPreferences(context: Context, name: string): Promise&lt;Preferences&gt;
P
PaDoBoo 已提交
59 60 61

读取指定文件,将数据加载到Preferences实例,用于数据操作,使用Promise方式作为异步方法。

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

P
PaDoBoo 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |

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

- 示例:
  ```
P
PaDaBoo 已提交
77 78
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
79
  var path = this.context.getDataBaseDir()
P
PaDaBoo 已提交
80
  let promise = data_Preferences.getPreferences(this.context, 'mystore')
P
PaDoBoo 已提交
81 82 83 84 85 86 87 88 89
  promise.then((preferences) => {
      preferences.putSync('startup', 'auto')
      preferences.flushSync()
  }).catch((err) => {
      console.info("Get the preferences failed, path: " + path + '/mystore')
  })
  ```


P
PaDaBoo 已提交
90
## data_Preferences.deletePreferences
P
PaDoBoo 已提交
91

P
PaDaBoo 已提交
92
deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;)
P
PaDoBoo 已提交
93 94 95

从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式作为异步方法。

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

P
PaDoBoo 已提交
98 99 100
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
101 102
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
103 104 105
  | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

- 示例:
106
  ```
P
PaDaBoo 已提交
107 108 109
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
  data_Preferences.deletePreferences(this.context, 'mystore', function (err) {
P
PaDoBoo 已提交
110 111 112 113 114 115 116 117 118
      if (err) {
          console.info("Deleted failed with err: " + err)
          return
      }
      console.info("Deleted successfully.")
  })
  ```


P
PaDaBoo 已提交
119
## data_Preferences.deletePreferences
P
PaDoBoo 已提交
120

P
PaDaBoo 已提交
121
deletePreferences(context: Context, name: string): Promise&lt;void&gt;
P
PaDoBoo 已提交
122 123 124

从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式作为异步方法。

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

P
PaDoBoo 已提交
127 128 129
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
130 131
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
132 133 134 135 136 137 138 139

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

- 示例:
  ```
P
PaDaBoo 已提交
140 141 142
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
  let promise = data_Preferences.deletePreferences(this.context, 'mystore')
P
PaDoBoo 已提交
143 144 145 146 147 148 149 150
  promise.then(() => {
      console.info("Deleted successfully.")
  }).catch((err) => {
      console.info("Deleted failed with err: " + err)
  })
  ```


P
PaDaBoo 已提交
151
## data_Preferences.removePreferencesFromCache
P
PaDoBoo 已提交
152

P
PaDaBoo 已提交
153
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void
P
PaDoBoo 已提交
154 155 156 157 158

从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。

此方法为异步方法。

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

P
PaDoBoo 已提交
161 162 163
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
164 165
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
166 167 168 169
  | callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | 是 | 回调函数。 |

- 示例:
  ```
P
PaDaBoo 已提交
170 171 172
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
  data_Preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
P
PaDoBoo 已提交
173 174 175 176 177 178 179 180 181
      if (err) {
          console.info("Removed preferences from cache failed with err: " + err)
          return
      }
      console.info("Removed preferences from cache successfully.")
  })
  ```


P
PaDaBoo 已提交
182
## data_Preferences.removePreferencesFromCache
P
PaDoBoo 已提交
183

P
PaDaBoo 已提交
184
removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;
P
PaDoBoo 已提交
185 186 187 188 189

从内存中移除指定文件对应的Preferences单实例。移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。

此方法为异步方法。

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

P
PaDoBoo 已提交
192 193 194
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
P
PaDaBoo 已提交
195 196
  | context | Context | 是 | 应用程序或功能的上下文 |
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
197 198 199 200 201 202 203 204

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

- 示例:
  ```
P
PaDaBoo 已提交
205 206 207
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
  let promise = data_Preferences.removePreferencesFromCache(this.context, 'mystore')
P
PaDoBoo 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  promise.then(() => {
      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&lt;ValueType&gt;): void

获取键对应的值,如果值为null或者非默认值类型,返回默认数据。

此方法为异步方法。

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

P
PaDoBoo 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称。它不能为空。 |
  | defValue | ValueType | 是 | 默认返回值。支持number、string、boolean。 |
  | callback | AsyncCallback&lt;ValueType&gt; | 是 | 回调函数。 |

- 示例:
  ```
  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&lt;ValueType&gt;

获取键对应的值,如果值为null或者非默认值类型,返默认数据。

此方法为异步方法。

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

P
PaDoBoo 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
- **参数:**
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称。它不能为空。 |
  | defValue | ValueType | 是 | 默认返回值。支持number、string、boolean。 |

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

- 示例:
  ```
  let promise = preferences.get('startup', 'default')
  promise.then((value) => {
      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&lt;void&gt;): void

首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。

此方法为异步方法。

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

P
PaDoBoo 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要修改的存储的key。它不能为空。 |
  | value | ValueType | 是 | 存储的新值。支持number、string、boolean。 |
  | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

- 示例:
  ```
  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&lt;void&gt;

首先获取指定文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化。

此方法为异步方法。

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

P
PaDoBoo 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要修改的存储的key。它不能为空。 |
  | value | ValueType | 是 | 存储的新值。支持number、string、boolean。 |

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise实例,用于异步处理。 |

- 示例:
  ```
  let promise = preferences.put('startup', 'auto')
  promise.then(() => {
      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&lt;boolean&gt;): boolean

检查存储对象是否包含名为给定key的存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
  | callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。 |

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

- 示例:
  ```
  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&lt;boolean&gt;

检查存储对象是否包含名为给定key的存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称。它不能为空。 |

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise实例,用于异步处理。 |

- 示例:
  ```
  let promise = preferences.has('startup')
  promise.then((isExist) => {
      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&lt;void&gt;): void

从存储对象中删除名为给定key的存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
  | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

- 示例:
  ```
  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&lt;void&gt;

从存储对象删除名为给定key的存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | key | string | 是 | 要获取的存储key名称。 |

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise实例,用于异步处理。 |

- 示例:
  ```
  let promise = preferences.delete('startup')
  promise.then(() => {
      console.info("Deleted startup key successfully.")
  }).catch((err) => {
      console.info("Delete startup key failed with err: " + err)
  })
  ```


### flush

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

将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。

此方法为异步方法。

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

P
PaDoBoo 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

- 示例:
  ```
  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&lt;void&gt;

将当前preferences对象中的修改保存到当前的preferences,并异步存储到文件中。

此方法为异步方法。

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

P
PaDoBoo 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise实例,用于异步处理。 |

- 示例:
  ```
  let promise = preferences.flush()
  promise.then(() => {
      console.info("Flushed to file successfully.")
  }).catch((err) => {
      console.info("Flush to file failed with err: " + err)
  })
  ```


### clear

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

清除此存储对象中的所有存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

- 示例:
  ```
  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&lt;void&gt;

清除此存储对象中的所有存储。

此方法为异步方法。

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

P
PaDoBoo 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise实例,用于异步处理。 |

- 示例:
  ```
  let promise = preferences.clear()
  promise.then(() => {
      console.info("Cleared to file successfully.")
  }).catch((err) => {
      console.info("Clear to file failed with err: " + err)
  })
  ```


### on('change')

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

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

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

P
PaDoBoo 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback&lt;{ key : string }&gt; | 回调对象实例。 |

- 示例:
  ```
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  preferences.on('change', observer)
  preferences.put('startup', 'auto')
  preferences.flush()  // observer will be called.
  ```


### off('change')

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

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

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

P
PaDoBoo 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback&lt;{ key : string }&gt; | 需要取消的回调对象实例。 |

- 示例:
  ```
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  preferences.off('change', observer)
  ```