1# @ohos.data.storage (轻量级存储) 2<!--Kit: ArkData--> 3<!--Subsystem: DistributedDataManager--> 4<!--Owner: @yanhuii--> 5<!--Designer: @houpengtao1--> 6<!--Tester: @yippo; @logic42--> 7<!--Adviser: @ge-yafang--> 8 9轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 从API version 9开始,该接口不再维护,推荐使用新接口[`@ohos.data.preferences`](js-apis-data-preferences.md)。 16 17 18## 导入模块 19 20```js 21import data_storage from '@ohos.data.storage'; 22``` 23 24## 常量 25 26**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core 27 28| 名称 | 类型 | 可读 | 可写 | 说明 | 29| ---------------- | -------- | ---- | ---- | ------------------------------------- | 30| MAX_KEY_LENGTH | number | 是 | 否 | key的最大长度限制为80字节。 | 31| MAX_VALUE_LENGTH | number | 是 | 否 | value的最大长度限制为8192字节。 | 32 33 34## data_storage.getStorageSync 35 36getStorageSync(path: string): Storage 37 38读取指定文件,将数据加载到Storage实例,用于数据操作。 39 40**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| ------ | ------ | ---- | -------------------------- | 46| path | string | 是 | 应用程序内部数据存储路径。 | 47 48**返回值:** 49 50| 类型 | 说明 | 51| ------------------- | ------------------------------------------------- | 52| [Storage](#storage) | 获取到要操作的Storage实例,用于进行数据存储操作。 | 53 54**示例:** 55 56```js 57import featureAbility from '@ohos.ability.featureAbility'; 58 59let path; 60let context = featureAbility.getContext(); 61context.getFilesDir().then((filePath) => { 62 path = filePath; 63 console.info("======================>getFilesDirPromise====================>"); 64 65 let storage = data_storage.getStorageSync(path + '/mystore'); 66 storage.putSync('startup', 'auto'); 67 storage.flushSync(); 68}); 69``` 70 71 72## data_storage.getStorage 73 74getStorage(path: string, callback: AsyncCallback<Storage>): void 75 76读取指定文件,将数据加载到Storage实例,用于数据操作,使用callback方式返回结果,此方法为异步方法。 77 78**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 79 80**参数:** 81 82| 参数名 | 类型 | 必填 | 说明 | 83| -------- | ---------------------------------------- | ---- | -------------------------- | 84| path | string | 是 | 应用程序内部数据存储路径。 | 85| callback | AsyncCallback<[Storage](#storage)> | 是 | 回调函数。 | 86 87**示例:** 88 89```js 90import featureAbility from '@ohos.ability.featureAbility'; 91 92let path; 93let context = featureAbility.getContext(); 94context.getFilesDir().then((filePath) => { 95 path = filePath; 96 console.info("======================>getFilesDirPromise====================>"); 97 98 data_storage.getStorage(path + '/mystore', function (err, storage) { 99 if (err) { 100 console.info("Failed to get the storage. path: " + path + '/mystore'); 101 return; 102 } 103 storage.putSync('startup', 'auto'); 104 storage.flushSync(); 105 }) 106}); 107``` 108 109 110## data_storage.getStorage 111 112getStorage(path: string): Promise<Storage> 113 114读取指定文件,将数据加载到Storage实例,用于数据操作,使用Promise方式返回结果,此方法为异步方法。 115 116**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 117 118**参数:** 119 120| 参数名 | 类型 | 必填 | 说明 | 121| ------ | ------ | ---- | -------------------------- | 122| path | string | 是 | 应用程序内部数据存储路径。 | 123 124**返回值:** 125 126| 类型 | 说明 | 127| ---------------------------------- | ------------------------------- | 128| Promise<[Storage](#storage)> | Promise实例,用于异步获取结果。 | 129 130**示例:** 131 132```js 133import featureAbility from '@ohos.ability.featureAbility'; 134 135let path; 136let context = featureAbility.getContext(); 137context.getFilesDir().then((filePath) => { 138 path = filePath; 139 console.info("======================>getFilesDirPromise====================>"); 140 141 let getPromise = data_storage.getStorage(path + '/mystore'); 142 getPromise.then((storage) => { 143 storage.putSync('startup', 'auto'); 144 storage.flushSync(); 145 }).catch((err) => { 146 console.info("Failed to get the storage. path: " + path + '/mystore'); 147 }) 148}); 149``` 150 151 152## data_storage.deleteStorageSync 153 154deleteStorageSync(path: string): void 155 156从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 157 158**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 159 160**参数:** 161 162| 参数名 | 类型 | 必填 | 说明 | 163| ------ | ------ | ---- | -------------------------- | 164| path | string | 是 | 应用程序内部数据存储路径。 | 165 166**示例:** 167 168```js 169import featureAbility from '@ohos.ability.featureAbility'; 170 171let path; 172let context = featureAbility.getContext(); 173context.getFilesDir().then((filePath) => { 174 path = filePath; 175 console.info("======================>getFilesDirPromise====================>"); 176 177 data_storage.deleteStorageSync(path + '/mystore'); 178}); 179``` 180 181## data_storage.deleteStorage 182 183deleteStorage(path: string, callback: AsyncCallback<void>): void 184 185从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用callback方式返回结果,此方法为异步方法。 186 187**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| -------- | ------------------------- | ---- | -------------------------- | 193| path | string | 是 | 应用程序内部数据存储路径。 | 194| callback | AsyncCallback<void> | 是 | 回调函数。 | 195 196**示例:** 197 198```js 199import featureAbility from '@ohos.ability.featureAbility'; 200 201let path; 202let context = featureAbility.getContext(); 203context.getFilesDir().then((filePath) => { 204 path = filePath; 205 console.info("======================>getFilesDirPromise====================>"); 206 207 data_storage.deleteStorage(path + '/mystore', function (err) { 208 if (err) { 209 console.info("Failed to delete the storage with err: " + err); 210 return; 211 } 212 console.info("Succeeded in deleting the storage."); 213 }) 214}); 215``` 216 217 218## data_storage.deleteStorage 219 220deleteStorage(path: string): Promise<void> 221 222从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,使用promise方式返回结果,此方法为异步方法。 223 224**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 225 226**参数:** 227 228| 参数名 | 类型 | 必填 | 说明 | 229| ------ | ------ | ---- | -------------------------- | 230| path | string | 是 | 应用程序内部数据存储路径。 | 231 232**返回值:** 233 234| 类型 | 说明 | 235| ------------------- | ------------------------------- | 236| Promise<void> | Promise实例,用于异步获取结果。 | 237 238**示例:** 239 240```js 241import featureAbility from '@ohos.ability.featureAbility'; 242 243let path; 244let context = featureAbility.getContext(); 245context.getFilesDir().then((filePath) => { 246 path = filePath; 247 console.info("======================>getFilesDirPromise====================>"); 248 249 let promisedelSt = data_storage.deleteStorage(path + '/mystore'); 250 promisedelSt.then(() => { 251 console.info("Succeeded in deleting the storage."); 252 }).catch((err) => { 253 console.info("Failed to delete the storage with err: " + err); 254 }) 255}); 256``` 257 258 259## data_storage.removeStorageFromCacheSync 260 261removeStorageFromCacheSync(path: string): void 262 263从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 264 265**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 266 267**参数:** 268| 参数名 | 类型 | 必填 | 说明 | 269| ------ | ------ | ---- | -------------------------- | 270| path | string | 是 | 应用程序内部数据存储路径。 | 271 272**示例:** 273 274```js 275import featureAbility from '@ohos.ability.featureAbility'; 276 277let path; 278let context = featureAbility.getContext(); 279context.getFilesDir().then((filePath) => { 280 path = filePath; 281 console.info("======================>getFilesDirPromise====================>"); 282 283 data_storage.removeStorageFromCacheSync(path + '/mystore'); 284}); 285``` 286 287 288## data_storage.removeStorageFromCache 289 290removeStorageFromCache(path: string, callback: AsyncCallback<void>): void 291 292从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。使用callback方式返回结果,此方法为异步方法。 293 294**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 295 296**参数:** 297 298| 参数名 | 类型 | 必填 | 说明 | 299| -------- | ------------------------- | ---- | -------------------------- | 300| path | string | 是 | 应用程序内部数据存储路径。 | 301| callback | AsyncCallback<void> | 是 | 回调函数。 | 302 303**示例:** 304 305```js 306import featureAbility from '@ohos.ability.featureAbility'; 307 308let path; 309let context = featureAbility.getContext(); 310context.getFilesDir().then((filePath) => { 311 path = filePath; 312 console.info("======================>getFilesDirPromise====================>"); 313 314 data_storage.removeStorageFromCache(path + '/mystore', function (err) { 315 if (err) { 316 console.info("Failed to remove storage from cache with err: " + err); 317 return; 318 } 319 console.info("Succeeded in removing storage from cache."); 320 }) 321}); 322``` 323 324 325## data_storage.removeStorageFromCache 326 327removeStorageFromCache(path: string): Promise<void> 328 329从内存中移除指定文件对应的Storage单实例。移除Storage单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。使用Promise方式返回结果,此方法为异步方法。 330 331**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 332 333**参数:** 334 335| 参数名 | 类型 | 必填 | 说明 | 336| ------ | ------ | ---- | -------------------------- | 337| path | string | 是 | 应用程序内部数据存储路径。 | 338 339**返回值:** 340 341| 类型 | 说明 | 342| ------------------- | ------------------------------- | 343| Promise<void> | Promise实例,用于异步获取结果。 | 344 345**示例:** 346 347```js 348import featureAbility from '@ohos.ability.featureAbility'; 349 350let path; 351let context = featureAbility.getContext(); 352context.getFilesDir().then((filePath) => { 353 path = filePath; 354 console.info("======================>getFilesDirPromise====================>"); 355 356 let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore') 357 promiserevSt.then(() => { 358 console.info("Succeeded in removing storage from cache."); 359 }).catch((err) => { 360 console.info("Failed to remove storage from cache with err: " + err); 361 }) 362}); 363``` 364 365## Storage 366 367提供获取和修改存储数据的接口。 368 369下列接口都需先使用[data_storage.getStorage](#data_storagegetstorage)或[data_storage.getStorageSync](#data_storagegetstoragesync)获取到Storage实例,再通过此实例调用对应接口。 370 371### getSync 372 373getSync(key: string, defValue: ValueType): ValueType 374 375获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue。 376 377**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 378 379**参数:** 380 381| 参数名 | 类型 | 必填 | 说明 | 382| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 383| key | string | 是 | 要获取的存储key名称,不能为空。 | 384| defValue | [ValueType](#valuetype) | 是 | 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。 | 385 386**返回值:** 387 388| 类型 | 说明 | 389| --------- | -------------------------------------------------------- | 390| ValueType | 键对应的值,如果值为null或者非默认值类型,返回默认数据。 | 391 392**示例:** 393 394```js 395let value = storage.getSync('startup', 'default'); 396console.info("The value of startup is " + value); 397``` 398 399 400### get 401 402get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void 403 404获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue。使用callback方式返回结果,此方法为异步方法。 405 406**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 407 408**参数:** 409 410| 参数名 | 类型 | 必填 | 说明 | 411| -------- | ------------------------------ | ---- | ----------------------------------------- | 412| key | string | 是 | 要获取的存储key名称,不能为空。 | 413| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | 414| callback | AsyncCallback<ValueType> | 是 | 回调函数。 | 415 416**示例:** 417 418```js 419storage.get('startup', 'default', function(err, value) { 420 if (err) { 421 console.info("Failed to get the value of startup with err: " + err); 422 return; 423 } 424 console.info("The value of startup is " + value); 425}) 426``` 427 428 429### get 430 431get(key: string, defValue: ValueType): Promise<ValueType> 432 433获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue。使用Promise方式返回结果,此方法为异步方法。 434 435**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 436 437**参数:** 438 439| 参数名 | 类型 | 必填 | 说明 | 440| -------- | ----------------------- | ---- | ----------------------------------------- | 441| key | string | 是 | 要获取的存储key名称,不能为空。 | 442| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | 443 444**返回值:** 445 446| 类型 | 说明 | 447| ------------------------ | ------------------------------- | 448| Promise<ValueType> | Promise实例,用于异步获取结果。 | 449 450**示例:** 451 452```js 453let promiseget = storage.get('startup', 'default'); 454promiseget.then((value) => { 455 console.info("The value of startup is " + value) 456}).catch((err) => { 457 console.info("Failed to get the value of startup with err: " + err); 458}) 459``` 460 461 462### putSync 463 464putSync(key: string, value: ValueType): void 465 466首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。 467 468**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 469 470**参数:** 471 472| 参数名 | 类型 | 必填 | 说明 | 473| ------ | ----------------------- | ---- | ----------------------------------------- | 474| key | string | 是 | 要修改的存储的key,不能为空。 | 475| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | 476 477**示例:** 478 479```js 480storage.putSync('startup', 'auto'); 481``` 482 483 484### put 485 486put(key: string, value: ValueType, callback: AsyncCallback<void>): void 487 488首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。使用callback方式返回结果,此方法为异步方法。 489 490**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 491 492**参数:** 493 494| 参数名 | 类型 | 必填 | 说明 | 495| -------- | ------------------------- | ---- | ----------------------------------------- | 496| key | string | 是 | 要修改的存储的key,不能为空。 | 497| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | 498| callback | AsyncCallback<void> | 是 | 回调函数。 | 499 500**示例:** 501 502```js 503storage.put('startup', 'auto', function (err) { 504 if (err) { 505 console.info("Failed to put the value of startup with err: " + err); 506 return; 507 } 508 console.info("Succeeded in putting the value of startup."); 509}) 510``` 511 512 513### put 514 515put(key: string, value: ValueType): Promise<void> 516 517首先获取指定文件对应的Storage实例,然后借助Storage API将数据写入Storage实例,通过flush或者flushSync将Storage实例持久化。使用Promise方式返回结果,此方法为异步方法。 518 519**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 520 521**参数:** 522 523| 参数名 | 类型 | 必填 | 说明 | 524| ------ | ----------------------- | ---- | ----------------------------------------- | 525| key | string | 是 | 要修改的存储的key,不能为空。 | 526| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | 527 528**返回值:** 529 530| 类型 | 说明 | 531| ------------------- | --------------------------- | 532| Promise<void> | Promise实例,用于异步处理。 | 533 534**示例:** 535 536```js 537let promiseput = storage.put('startup', 'auto'); 538promiseput.then(() => { 539 console.info("Succeeded in putting the value of startup."); 540}).catch((err) => { 541 console.info("Failed to put the value of startup with err: " + err); 542}) 543``` 544 545 546### hasSync 547 548hasSync(key: string): boolean 549 550检查存储对象是否包含名为给定key的存储。 551 552**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 553 554**参数:** 555 556| 参数名 | 类型 | 必填 | 说明 | 557| ------ | ------ | ---- | ------------------------------- | 558| key | string | 是 | 要获取的存储key名称,不能为空。 | 559 560**返回值:** 561 562| 类型 | 说明 | 563| ------- | ------------------------------------- | 564| boolean | true 表示存在,false表示不存在。 | 565 566**示例:** 567 568```js 569let isExist = storage.hasSync('startup'); 570if (isExist) { 571 console.info("The key of startup is contained."); 572} 573``` 574 575 576### has 577 578has(key: string, callback: AsyncCallback<boolean>): boolean 579 580检查存储对象是否包含名为给定key的存储。使用callback方式返回结果,此方法为异步方法。 581 582**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 583 584**参数:** 585 586| 参数名 | 类型 | 必填 | 说明 | 587| -------- | ---------------------------- | ---- | ------------------------------- | 588| key | string | 是 | 要获取的存储key名称,不能为空。 | 589| callback | AsyncCallback<boolean> | 是 | 回调函数。 | 590 591**返回值:** 592 593| 类型 | 说明 | 594| ------- | ------------------------------- | 595| boolean | true表示存在,false表示不存在。 | 596 597**示例:** 598 599```js 600storage.has('startup', function (err, isExist) { 601 if (err) { 602 console.info("Failed to check the key of startup with err: " + err); 603 return; 604 } 605 if (isExist) { 606 console.info("The key of startup is contained."); 607 } 608}) 609``` 610 611 612### has 613 614has(key: string): Promise<boolean> 615 616检查存储对象是否包含名为给定key的存储。使用Promise方式返回结果,此方法为异步方法。 617 618**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 619 620**参数:** 621 622| 参数名 | 类型 | 必填 | 说明 | 623| ------ | ------ | ---- | ------------------------------- | 624| key | string | 是 | 要获取的存储key名称,不能为空。 | 625 626**返回值:** 627 628| 类型 | 说明 | 629| ---------------------- | --------------------------- | 630| Promise<boolean> | Promise实例,用于异步处理。 | 631 632**示例:** 633 634```js 635let promisehas = storage.has('startup') 636promisehas.then((isExist) => { 637 if (isExist) { 638 console.info("The key of startup is contained."); 639 } 640}).catch((err) => { 641 console.info("Failed to check the key of startup with err: " + err); 642}) 643``` 644 645 646### deleteSync 647 648deleteSync(key: string): void 649 650从存储对象中删除名为给定key的存储。 651 652**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 653 654**参数:** 655 656| 参数名 | 类型 | 必填 | 说明 | 657| ------ | ------ | ---- | --------------------------------- | 658| key | string | 是 | 要获取的存储key名称。它不能为空。 | 659 660**示例:** 661 662```js 663 storage.deleteSync('startup'); 664``` 665 666 667### delete 668 669delete(key: string, callback: AsyncCallback<void>): void 670 671从存储对象中删除名为给定key的存储。使用callback方式返回结果,此方法为异步方法。 672 673**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 674 675**参数:** 676 677| 参数名 | 类型 | 必填 | 说明 | 678| -------- | ------------------------- | ---- | ------------------------------- | 679| key | string | 是 | 要获取的存储key名称,不能为空。 | 680| callback | AsyncCallback<void> | 是 | 回调函数。 | 681 682**示例:** 683 684```js 685storage.delete('startup', function (err) { 686 if (err) { 687 console.info("Failed to delete startup key failed err: " + err); 688 return; 689 } 690 console.info("Succeeded in deleting startup key."); 691}) 692``` 693 694 695### delete 696 697delete(key: string): Promise<void> 698 699从存储对象删除名为给定key的存储。使用Promise方式返回结果,此方法为异步方法。 700 701**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 702 703**参数:** 704 705| 参数名 | 类型 | 必填 | 说明 | 706| ------ | ------ | ---- | --------------------- | 707| key | string | 是 | 要获取的存储key名称。 | 708 709**返回值:** 710 711| 类型 | 说明 | 712| ------------------- | --------------------------- | 713| Promise<void> | Promise实例,用于异步处理。 | 714 715**示例:** 716 717```js 718let promisedel = storage.delete('startup') 719promisedel.then(() => { 720 console.info("Succeeded in deleting startup key."); 721}).catch((err) => { 722 console.info("Failed to delete startup key failed err: " + err); 723}) 724``` 725 726 727### flushSync 728 729flushSync(): void 730 731将当前storage对象中的修改保存到当前的storage,并同步存储到文件中。 732 733**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 734 735**示例:** 736 737```js 738storage.flushSync(); 739``` 740 741 742### flush 743 744flush(callback: AsyncCallback<void>): void 745 746将当前storage对象中的修改保存到当前的storage,并异步存储到文件中。使用callback方式返回结果,此方法为异步方法。 747 748**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 749 750**参数:** 751 752| 参数名 | 类型 | 必填 | 说明 | 753| -------- | ------------------------- | ---- | ---------- | 754| callback | AsyncCallback<void> | 是 | 回调函数。 | 755 756**示例:** 757 758```js 759storage.flush(function (err) { 760 if (err) { 761 console.info("Failed to flush to file with err: " + err); 762 return; 763 } 764 console.info("Succeeded in flushing to file."); 765}) 766``` 767 768 769### flush 770 771flush(): Promise<void> 772 773将当前storage对象中的修改保存到当前的storage,并异步存储到文件中。使用Promise方式返回结果,此方法为异步方法。 774 775**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 776 777**返回值:** 778 779| 类型 | 说明 | 780| ------------------- | --------------------------- | 781| Promise<void> | Promise实例,用于异步处理。 | 782 783**示例:** 784 785```js 786let promiseflush = storage.flush(); 787promiseflush.then(() => { 788 console.info("Succeeded in flushing to file."); 789}).catch((err) => { 790 console.info("Failed to flush to file with err: " + err); 791}) 792``` 793 794 795### clearSync 796 797clearSync(): void 798 799清除此存储对象中的所有存储。 800 801**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 802 803**示例:** 804 805```js 806storage.clearSync(); 807``` 808 809 810### clear 811 812clear(callback: AsyncCallback<void>): void 813 814清除此存储对象中的所有存储。使用callback方式返回结果,此方法为异步方法。 815 816**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 817 818**参数:** 819 820| 参数名 | 类型 | 必填 | 说明 | 821| -------- | ------------------------- | ---- | ---------- | 822| callback | AsyncCallback<void> | 是 | 回调函数。 | 823 824**示例:** 825 826```js 827storage.clear(function (err) { 828 if (err) { 829 console.info("Failed to clear the storage with err: " + err); 830 return; 831 } 832 console.info("Succeeded in clearing the storage."); 833}) 834``` 835 836 837### clear 838 839clear(): Promise<void> 840 841清除此存储对象中的所有存储。使用Promise方式返回结果,此方法为异步方法。 842 843**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 844 845**返回值:** 846| 类型 | 说明 | 847| ------------------- | --------------------------- | 848| Promise<void> | Promise实例,用于异步处理。 | 849 850**示例:** 851 852```js 853let promiseclear = storage.clear(); 854promiseclear.then(() => { 855 console.info("Succeeded in clearing the storage."); 856}).catch((err) => { 857 console.info("Failed to clear the storage with err: " + err); 858}) 859``` 860 861 862### on('change') 863 864on(type: 'change', callback: Callback<StorageObserver>): void 865 866订阅数据变更者类需要实现StorageObserver接口,订阅的key的值发生变更后,在执行flush/flushSync方法后,callback方法会被回调。 867 868**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 869 870**参数:** 871 872| 参数名 | 类型 | 必填| 说明 | 873| -------- | --------------------------------------------------- | ------ |---------------------------------------- | 874| type | string |是| 事件类型,固定值'change',表示数据变更。 | 875| callback | Callback<[StorageObserver](#storageobserver)> | 是|回调对象实例。 | 876 877**示例:** 878 879```js 880let observer = function (key) { 881 console.info("The key of " + key + " changed."); 882} 883storage.on('change', observer); 884storage.putSync('startup', 'auto'); 885storage.flushSync(); // observer will be called. 886``` 887 888 889### off('change') 890 891off(type: 'change', callback: Callback<StorageObserver>): void 892 893当不再进行订阅数据变更时,使用此接口取消订阅。 894 895**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 896 897**参数:** 898 899| 参数名 | 类型 | 必填 | 说明 | 900| -------- | --------------------------------------------------- | ------ |---------------------------------------- | 901| type | string |是| 事件类型,固定值'change',表示数据变更。 | 902| callback | Callback<[StorageObserver](#storageobserver)> | 是|需要取消的回调对象实例。 | 903 904**示例:** 905 906```js 907let observer = function (key) { 908 console.info("The key of " + key + " changed."); 909} 910storage.off('change', observer); 911``` 912 913 914## StorageObserver 915 916**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 917 918| 名称 | 类型 | 必填 | 说明 | 919| ---- | -------- | ---- | ---------------- | 920| key | string | 是 | 变更的数据内容。 | 921 922## ValueType 923 924type ValueType = number | string | boolean 925 926用于表示允许的数据字段类型。 927 928**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 929 930| 类型 | 说明 | 931| ------- | -------------------- | 932| number | 表示值类型为数字。 | 933| string | 表示值类型为字符。 | 934| boolean | 表示值类型为布尔值。 | 935