js-apis-data-preferences.md 18.6 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
```

## 属性

| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
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
### 系统能力 ###
31 32
SystemCapability.DistributedDataManager.Preferences.Core

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 44 45
  import data_Preferences from '@ohos.data.preferences'
  var path = await this.context.getDataBaseDir()
  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
### 系统能力 ###
63 64
SystemCapability.DistributedDataManager.Preferences.Core

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

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

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


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

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

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

P
PaDaBoo 已提交
97
### 系统能力 ###
98 99
SystemCapability.DistributedDataManager.Preferences.Core

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

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


P
PaDaBoo 已提交
121
## data_Preferences.deletePreferences
P
PaDoBoo 已提交
122

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

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

P
PaDaBoo 已提交
127
### 系统能力 ###
128 129
SystemCapability.DistributedDataManager.Preferences.Core

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

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

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


P
PaDaBoo 已提交
154
## data_Preferences.removePreferencesFromCache
P
PaDoBoo 已提交
155

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

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

此方法为异步方法。

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

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

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


P
PaDaBoo 已提交
186
## data_Preferences.removePreferencesFromCache
P
PaDoBoo 已提交
187

P
PaDaBoo 已提交
188
removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;
P
PaDoBoo 已提交
189 190 191 192 193

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

此方法为异步方法。

P
PaDaBoo 已提交
194
### 系统能力 ###
195 196
SystemCapability.DistributedDataManager.Preferences.Core

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

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

- 示例:
  ```
P
PaDaBoo 已提交
210 211 212
  import Ability from '@ohos.application.Ability'
  import data_Preferences from '@ohos.data.preferences'
  let promise = data_Preferences.removePreferencesFromCache(this.context, 'mystore')
P
PaDoBoo 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  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 已提交
234
### 系统能力 ###
235 236
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
264
### 系统能力 ###
265 266
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
- **参数:**
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
297
### 系统能力 ###
298 299
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
327
### 系统能力 ###
328 329
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
360
### 系统能力 ###
361 362
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
396
### 系统能力 ###
397 398
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
430
### 系统能力 ###
431 432
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
459
### 系统能力 ###
460 461
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
491
### 系统能力 ###
492 493
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
519
### 系统能力 ###
520 521
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | 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 已提交
546
### 系统能力 ###
547 548
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | 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 已提交
574
### 系统能力 ###
575 576
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | 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 已提交
599
### 系统能力 ###
600 601
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | 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 已提交
625
### 系统能力 ###
626 627
SystemCapability.DistributedDataManager.Preferences.Core

P
PaDoBoo 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640
- 参数:
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback&lt;{ key : string }&gt; | 需要取消的回调对象实例。 |

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