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