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