提交 7bd1c9d6 编写于 作者: A annie_wangli

update docs

Signed-off-by: Nannie_wangli <annie.wangli@huawei.com>
上级 fc1a5ff7
...@@ -22,12 +22,13 @@ ...@@ -22,12 +22,13 @@
- [User Authentication](js-apis-useriam-userauth.md) - [User Authentication](js-apis-useriam-userauth.md)
- [Access Control](js-apis-abilityAccessCtrl.md) - [Access Control](js-apis-abilityAccessCtrl.md)
- Data Management - Data Management
- [Lightweight Storage (deprecated since 8)](js-apis-data-storage.md) - [Lightweight Storage<sup>9+</sup>](js-apis-data-preferences.md)
- [Lightweight Storage](js-apis-data-storage.md)
- [Distributed Data Management](js-apis-distributed-data.md) - [Distributed Data Management](js-apis-distributed-data.md)
- [Relational Database](js-apis-data-rdb.md) - [Relational Database](js-apis-data-rdb.md)
- [Result Set](js-apis-data-resultset.md) - [Result Set](js-apis-data-resultset.md)
- [DataAbilityPredicates](js-apis-data-ability.md) - [DataAbilityPredicates](js-apis-data-ability.md)
- [Settings](js-apis-settings.md) - [Settings](js-apis-settings.md)
- File Management - File Management
- [File Management](js-apis-fileio.md) - [File Management](js-apis-fileio.md)
- [Statfs](js-apis-statfs.md) - [Statfs](js-apis-statfs.md)
......
# Lightweight Storage
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import data_Preferences from '@ohos.data.preferences'
```
## Attributes
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It is 80 bytes.|
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value of the string type. It is 8192 bytes.|
## data_Preferences.getPreferences
getPreferences(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void
Reads a file and loads the data to the **Preferences** instance. This method uses an asynchronous callback to return the execution result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
| callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the execution result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
var path = this.context.getDataBaseDir()
data_Preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Get the preferences failed, path: " + path + '/mystore')
return;
}
preferences.putSync('startup', 'auto')
preferences.flushSync()
})
```
## data_Preferences.getPreferences
getPreferences(context: Context, name: string): Promise&lt;Preferences&gt;
Reads a file and loads the data to the **Preferences** instance. This method uses a promise to return the execution result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
var path = this.context.getDataBaseDir()
let promisePre = data_Preferences.getPreferences(this.context, 'mystore')
promisePre.then((preferences) => {
preferences.putSync('startup', 'auto')
preferences.flushSync()
}).catch((err) => {
console.info("Get the preferences failed, path: " + path + '/mystore')
})
```
## data_Preferences.deletePreferences
deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
Removes the singleton **Preferences** instance of the specified file from the memory, and deletes the specified file, its backup file, and corrupted files. After the specified files are deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses an asynchronous callback to return the execution result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
data_Preferences.deletePreferences(this.context, 'mystore', function (err) {
if (err) {
console.info("Deleted failed with err: " + err)
return
}
console.info("Deleted successfully.")
})
```
## data_Preferences.deletePreferences
deletePreferences(context: Context, name: string): Promise&lt;void&gt;
Removes the singleton **Preferences** instance of the specified file from the memory, and deletes the specified file, its backup file, and corrupted files. After the specified files are deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses a promise to return the execution result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
let promisedelPre = data_Preferences.deletePreferences(this.context, 'mystore')
promisedelPre.then(() => {
console.info("Deleted successfully.")
}).catch((err) => {
console.info("Deleted failed with err: " + err)
})
```
## data_Preferences.removePreferencesFromCache
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
Removes the singleton **Preferences** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
data_Preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
if (err) {
console.info("Removed preferences from cache failed with err: " + err)
return
}
console.info("Removed preferences from cache successfully.")
})
```
## data_Preferences.removePreferencesFromCache
removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;
Removes the singleton **Preferences** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
import Ability from '@ohos.application.Ability'
import data_Preferences from '@ohos.data.preferences'
let promiserevPre = data_Preferences.removePreferencesFromCache(this.context, 'mystore')
promiserevPre.then(() => {
console.info("Removed preferences from cache successfully.")
}).catch((err) => {
console.info("Removed preferences from cache failed with err: " + err)
})
```
## Preferences
Provides APIs for obtaining and modifying storage data.
### get
get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| defValue | ValueType | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|
- Example
```
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;
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.
This method uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| defValue | ValueType | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;ValueType&gt; | Promise used to return the result.|
- Example
```
let promiseget = preferences.get('startup', 'default')
promiseget.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
Obtains the **Preferences** instance corresponding to the specified file, writes data to the **Preferences** instance using a **Preferences** API, and saves data to the file using **flush()** or **flushSync()**.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to modify. It cannot be empty.|
| value | ValueType | Yes| New value to store. It can be a number, string, or Boolean value.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
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;
Obtains the **Preferences** instance corresponding to the specified file, writes data to the **Preferences** instance using a **Preferences** API, and saves data to the file using **flush()** or **flushSync()**.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to modify. It cannot be empty.|
| value | ValueType | Yes| New value to store. It can be a number, string, or Boolean value.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promiseput = preferences.put('startup', 'auto')
promiseput.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
Checks whether the **Preference** object contains data with a given key.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|
- Return value
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the **Preference** object contains data with the specified key; returns **false** otherwise.|
- Example
```
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;
Checks whether the **Preference** object contains data with a given key.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to return the result.|
- Example
```
let promisehas = preferences.has('startup')
promisehas.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
Deletes data with the specified key from this **Preference** object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
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;
Deletes data with the specified key from this **Preference** object.
This method uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promisedel = preferences.delete('startup')
promisedel.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
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
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;
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promiseflush = preferences.flush()
promiseflush.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
Clears this **Preferences** object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
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;
Clears this **Preferences** object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promiseclear = preferences.clear()
promiseclear.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
Subscribes to data changes. When the value of the subscribed key changes, a callback will be invoked after the **flush()** method is executed.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Description|
| -------- | -------- | -------- |
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
- Example
```
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
Unsubscribes from data changes.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Description|
| -------- | -------- | -------- |
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
- Example
```
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
preferences.off('change', observer)
```
# Lightweight Storage<a name="EN-US_TOPIC_0000001154532408"></a> # Lightweight Storage
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value pairs. Keys are of the string type, and values can be of the numeric, string, or Boolean type. Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
>![](../../public_sys-resources/icon-note.gif) **NOTE**
>
>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import<a name="s56d19203690d4782bfc74069abb6bd71"></a> > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. <br/>The APIs of this module are no longer maintained since API Version 9. You are advised to use [@ohos.data.preferences](js-apis-data-preferences.md).
## Modules to Import
``` ```
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage'
``` ```
## System Capabilities ## Attributes
SystemCapability.DistributedDataManager.Preferences.Core
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
## Attributes<a name="section7299123218370"></a> | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
<a name="table1540155452420"></a> | MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It is 80 bytes.|
<table><thead align="left"><tr id="row1947713549244"><th class="cellrowborder" valign="top" width="22.720000000000002%" id="mcps1.1.6.1.1"><p id="p74771754102410"><a name="p74771754102410"></a><a name="p74771754102410"></a>Name</p> | MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value of the string type. It is 8192 bytes.|
</th>
<th class="cellrowborder" valign="top" width="12.839999999999998%" id="mcps1.1.6.1.2"><p id="p1047755412411"><a name="p1047755412411"></a><a name="p1047755412411"></a>Type</p>
</th> ## dataStorage.getStorageSync
<th class="cellrowborder" valign="top" width="10.97%" id="mcps1.1.6.1.3"><p id="p34782054192412"><a name="p34782054192412"></a><a name="p34782054192412"></a>Readable</p>
</th> getStorageSync(path: string): Storage
<th class="cellrowborder" valign="top" width="8.95%" id="mcps1.1.6.1.4"><p id="p1847865452413"><a name="p1847865452413"></a><a name="p1847865452413"></a>Writable</p>
</th> Reads a file and loads the data to the **Storage** instance in synchronous mode.
<th class="cellrowborder" valign="top" width="44.519999999999996%" id="mcps1.1.6.1.5"><p id="p12478954132419"><a name="p12478954132419"></a><a name="p12478954132419"></a>Description</p>
</th> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</tr>
</thead> - Parameters
<tbody><tr id="row1247811541240"><td class="cellrowborder" valign="top" width="22.720000000000002%" headers="mcps1.1.6.1.1 "><p id="p51525360147"><a name="p51525360147"></a><a name="p51525360147"></a>MAX_KEY_LENGTH</p> | Name| Type| Mandatory| Description|
</td> | -------- | -------- | -------- | -------- |
<td class="cellrowborder" valign="top" width="12.839999999999998%" headers="mcps1.1.6.1.2 "><p id="p9478135414243"><a name="p9478135414243"></a><a name="p9478135414243"></a>string</p> | path | string | Yes| Path of the target file.|
</td>
<td class="cellrowborder" valign="top" width="10.97%" headers="mcps1.1.6.1.3 "><p id="p1247885492411"><a name="p1247885492411"></a><a name="p1247885492411"></a>Yes</p> - Return value
</td> | Type| Description|
<td class="cellrowborder" valign="top" width="8.95%" headers="mcps1.1.6.1.4 "><p id="p3478054192416"><a name="p3478054192416"></a><a name="p3478054192416"></a>No</p> | -------- | -------- |
</td> | [Storage](#storage) | **Storage** instance used for data storage operations.|
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.1.6.1.5 "><p id="p12478654172415"><a name="p12478654172415"></a><a name="p12478654172415"></a>Maximum length of a key. It is 80 bytes.</p>
</td> - Example
</tr> ```
<tr id="row184789546249"><td class="cellrowborder" valign="top" width="22.720000000000002%" headers="mcps1.1.6.1.1 "><p id="p1971014525154"><a name="p1971014525154"></a><a name="p1971014525154"></a>MAX_VALUE_LENGTH</p> import dataStorage from '@ohos.data.storage'
</td> import featureAbility from '@ohos.ability.featureAbility'
<td class="cellrowborder" valign="top" width="12.839999999999998%" headers="mcps1.1.6.1.2 "><p id="p1392172791820"><a name="p1392172791820"></a><a name="p1392172791820"></a>string</p>
</td> var context = featureAbility.getContext()
<td class="cellrowborder" valign="top" width="10.97%" headers="mcps1.1.6.1.3 "><p id="p1870905231512"><a name="p1870905231512"></a><a name="p1870905231512"></a>Yes</p> var path = await context.getFilesDir()
</td> let storage = dataStorage.getStorageSync(path + '/mystore')
<td class="cellrowborder" valign="top" width="8.95%" headers="mcps1.1.6.1.4 "><p id="p137081152141516"><a name="p137081152141516"></a><a name="p137081152141516"></a>No</p> storage.putSync('startup', 'auto')
</td> storage.flushSync()
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.1.6.1.5 "><p id="p1470645211154"><a name="p1470645211154"></a><a name="p1470645211154"></a>Maximum length of a value of the string type. It is 8192 bytes.</p> ```
</td>
</tr>
</tbody> ## dataStorage.getStorage
</table>
getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void
## dataStorage.getStorageSync<a name="section172447329132"></a>
Reads a file and loads the data to the **Storage** instance. This method uses an asynchronous callback to return the execution result.
getStorageSync\(path: string\): Storage
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Reads a specified file and loads the data to the **Storage** instance for data operations in synchronous mode.
- Parameters
- Parameters | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
<a name="table2720134112170"></a> | path | string | Yes| Path of the target file.|
<table><thead align="left"><tr id="row18721041131710"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p19721184111716"><a name="p19721184111716"></a><a name="p19721184111716"></a>Name</p> | callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes| Callback used to return the execution result.|
</th>
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p672184171715"><a name="p672184171715"></a><a name="p672184171715"></a>Type</p> - Example
</th> ```
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p972174116173"><a name="p972174116173"></a><a name="p972174116173"></a>Mandatory</p> import dataStorage from '@ohos.data.storage'
</th> import featureAbility from '@ohos.ability.featureAbility'
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p1372134161712"><a name="p1372134161712"></a><a name="p1372134161712"></a>Description</p>
</th> var context = featureAbility.getContext()
</tr> var path = await context.getFilesDir()
</thead> dataStorage.getStorage(path + '/mystore', function (err, storage) {
<tbody><tr id="row11721541121717"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p1472154117175"><a name="p1472154117175"></a><a name="p1472154117175"></a>path</p> if (err) {
</td> console.info("Get the storage failed, path: " + path + '/mystore')
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p372134110175"><a name="p372134110175"></a><a name="p372134110175"></a>string</p> return;
</td> }
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p19721134101713"><a name="p19721134101713"></a><a name="p19721134101713"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1068312615010"><a name="p1068312615010"></a><a name="p1068312615010"></a>Storage path of the application internal data</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table372204131719"></a>
<table><thead align="left"><tr id="row8722124116175"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p4722041171712"><a name="p4722041171712"></a><a name="p4722041171712"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p072234114173"><a name="p072234114173"></a><a name="p072234114173"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row4722144111715"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p16723164161713"><a name="p16723164161713"></a><a name="p16723164161713"></a><a href="#section12882825611">Storage</a></p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p12723741161713"><a name="p12723741161713"></a><a name="p12723741161713"></a><strong id="b1665811064019"><a name="b1665811064019"></a><a name="b1665811064019"></a>Storage</strong> instance used for data storage operations</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
(async () => {
var context = featureAbility.getContext()
var path = await context.getFilesDir()
let storage = dataStorage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto')
storage.flushSync() storage.flushSync()
})() })
``` ```
## dataStorage.getStorage<a name="section192192415554"></a> ## dataStorage.getStorage
getStorage\(path: string, callback: AsyncCallback<Storage\>\): void getStorage(path: string): Promise&lt;Storage&gt;
Reads a specified file and loads the data to the **Storage** instance for data operations. This method uses a callback to return the execution result. Reads a file and loads the data to the **Storage** instance. This method uses a promise to return the execution result.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table69661135912"></a> - Parameters
<table><thead align="left"><tr id="row149668318915"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p7966738914"><a name="p7966738914"></a><a name="p7966738914"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="25.42174549000487%" id="mcps1.1.5.1.2"><p id="p296713699"><a name="p296713699"></a><a name="p296713699"></a>Type</p> | path | string | Yes| Path of the target file.|
</th>
<th class="cellrowborder" valign="top" width="10.511945392491468%" id="mcps1.1.5.1.3"><p id="p196718315911"><a name="p196718315911"></a><a name="p196718315911"></a>Mandatory</p> - Return value
</th> | Type| Description|
<th class="cellrowborder" valign="top" width="49.614822038030226%" id="mcps1.1.5.1.4"><p id="p9967231197"><a name="p9967231197"></a><a name="p9967231197"></a>Description</p> | -------- | -------- |
</th> | Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|
</tr>
</thead> - Example
<tbody><tr id="row99671533914"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p79671633910"><a name="p79671633910"></a><a name="p79671633910"></a>path</p> ```
</td> import dataStorage from '@ohos.data.storage'
<td class="cellrowborder" valign="top" width="25.42174549000487%" headers="mcps1.1.5.1.2 "><p id="p11967433914"><a name="p11967433914"></a><a name="p11967433914"></a>string</p> import featureAbility from '@ohos.ability.featureAbility'
</td>
<td class="cellrowborder" valign="top" width="10.511945392491468%" headers="mcps1.1.5.1.3 "><p id="p19671336916"><a name="p19671336916"></a><a name="p19671336916"></a>Yes</p> var context = featureAbility.getContext()
</td> var path = await context.getFilesDir()
<td class="cellrowborder" valign="top" width="49.614822038030226%" headers="mcps1.1.5.1.4 "><p id="p77021147102715"><a name="p77021147102715"></a><a name="p77021147102715"></a>Storage path of the application internal data</p> let promisegetSt = dataStorage.getStorage(path + '/mystore')
</td> promisegetSt.then((storage) => {
</tr> storage.putSync('startup', 'auto')
<tr id="row128961021101812"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p6897172121814"><a name="p6897172121814"></a><a name="p6897172121814"></a>callback</p> storage.flushSync()
</td> }).catch((err) => {
<td class="cellrowborder" valign="top" width="25.42174549000487%" headers="mcps1.1.5.1.2 "><p id="p9897102181813"><a name="p9897102181813"></a><a name="p9897102181813"></a>AsyncCallback&lt;<a href="#section12882825611">Storage</a>&gt;</p> console.info("Get the storage failed, path: " + path + '/mystore')
</td> })
<td class="cellrowborder" valign="top" width="10.511945392491468%" headers="mcps1.1.5.1.3 "><p id="p188982219189"><a name="p188982219189"></a><a name="p188982219189"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="49.614822038030226%" headers="mcps1.1.5.1.4 "><p id="p989815213187"><a name="p989815213187"></a><a name="p989815213187"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
(async () => {
var context = featureAbility.getContext()
var path = await context.getFilesDir()
dataStorage.getStorage(path + '/mystore', function (err, storage) {
if (err) {
console.info("Get the storage failed, path: " + path + '/mystore')
return;
}
storage.putSync('startup', 'auto')
storage.flushSync()
})
})()
``` ```
## dataStorage.getStorage<a name="section761705115251"></a> ## dataStorage.deleteStorageSync
getStorage\(path: string\): Promise<Storage\> deleteStorageSync(path: string): void
Reads a specified file and loads the data to the **Storage** instance for data operations. This method uses a promise to return the execution result. Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses a synchronous mode.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table206180511253"></a> - Parameters
<table><thead align="left"><tr id="row126181951152513"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p20618155112513"><a name="p20618155112513"></a><a name="p20618155112513"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="13.700633837152608%" id="mcps1.1.5.1.2"><p id="p561885114256"><a name="p561885114256"></a><a name="p561885114256"></a>Type</p> | path | string | Yes| Path of the target file.|
</th>
<th class="cellrowborder" valign="top" width="10.628961482203803%" id="mcps1.1.5.1.3"><p id="p561814519259"><a name="p561814519259"></a><a name="p561814519259"></a>Mandatory</p> - Example
</th> ```
<th class="cellrowborder" valign="top" width="61.21891760117016%" id="mcps1.1.5.1.4"><p id="p1861845120252"><a name="p1861845120252"></a><a name="p1861845120252"></a>Description</p> dataStorage.deleteStorageSync(path + '/mystore')
</th> ```
</tr>
</thead>
<tbody><tr id="row6619751162515"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p196192514253"><a name="p196192514253"></a><a name="p196192514253"></a>path</p> ## dataStorage.deleteStorage
</td>
<td class="cellrowborder" valign="top" width="13.700633837152608%" headers="mcps1.1.5.1.2 "><p id="p36191951162515"><a name="p36191951162515"></a><a name="p36191951162515"></a>string</p> deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
</td>
<td class="cellrowborder" valign="top" width="10.628961482203803%" headers="mcps1.1.5.1.3 "><p id="p16191051162511"><a name="p16191051162511"></a><a name="p16191051162511"></a>Yes</p> Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses an asynchronous callback to return the execution result.
</td>
<td class="cellrowborder" valign="top" width="61.21891760117016%" headers="mcps1.1.5.1.4 "><p id="p7722105418272"><a name="p7722105418272"></a><a name="p7722105418272"></a>Storage path of the application internal data</p> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</td>
</tr> - Parameters
</tbody> | Name| Type| Mandatory| Description|
</table> | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
- Return values | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
<a name="table2619551122516"></a> - Example
<table><thead align="left"><tr id="row46201751152513"><th class="cellrowborder" valign="top" width="30.44%" id="mcps1.1.3.1.1"><p id="p1162075110255"><a name="p1162075110255"></a><a name="p1162075110255"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="69.56%" id="mcps1.1.3.1.2"><p id="p46202512251"><a name="p46202512251"></a><a name="p46202512251"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row562015514258"><td class="cellrowborder" valign="top" width="30.44%" headers="mcps1.1.3.1.1 "><p id="p146201551122516"><a name="p146201551122516"></a><a name="p146201551122516"></a>Promise&lt;<a href="#section12882825611">Storage</a>&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p1062005172514"><a name="p1062005172514"></a><a name="p1062005172514"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
(async () => {
var context = featureAbility.getContext()
var path = await context.getFilesDir()
let promise = dataStorage.getStorage(path + '/mystore')
promise.then((storage) => {
storage.putSync('startup', 'auto')
storage.flushSync()
}).catch((err) => {
console.info("Get the storage failed, path: " + path + '/mystore')
})
}()
``` ```
dataStorage.deleteStorage(path + '/mystore', function (err) {
if (err) {
console.info("Deleted failed with err: " + err)
return
}
console.info("Deleted successfully.")
})
```
## dataStorage.deleteStorage
## dataStorage.deleteStorageSync<a name="section1396463815379"></a> deleteStorage(path: string): Promise&lt;void&gt;
deleteStorageSync\(path: string\): void Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses a promise to return the execution result.
Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses a synchronous mode. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters - Parameters
| Name| Type| Mandatory| Description|
<a name="table99651538193711"></a> | -------- | -------- | -------- | -------- |
<table><thead align="left"><tr id="row1296553843712"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p19965183833714"><a name="p19965183833714"></a><a name="p19965183833714"></a>Name</p> | path | string | Yes| Path of the target file.|
</th>
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p9965153863713"><a name="p9965153863713"></a><a name="p9965153863713"></a>Type</p> - Return value
</th> | Type| Description|
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p15965133819375"><a name="p15965133819375"></a><a name="p15965133819375"></a>Mandatory</p> | -------- | -------- |
</th> | Promise&lt;void&gt; | Promise used to return the result.|
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p496518383377"><a name="p496518383377"></a><a name="p496518383377"></a>Description</p>
</th> - Example
</tr> ```
</thead> let promisedelSt = dataStorage.deleteStorage(path + '/mystore')
<tbody><tr id="row6965238143718"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p18965163816378"><a name="p18965163816378"></a><a name="p18965163816378"></a>path</p> promisedelSt.then(() => {
</td> console.info("Deleted successfully.")
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p10965193816372"><a name="p10965193816372"></a><a name="p10965193816372"></a>string</p> }).catch((err) => {
</td> console.info("Deleted failed with err: " + err)
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p896623833719"><a name="p896623833719"></a><a name="p896623833719"></a>Yes</p> })
</td> ```
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p157012058162714"><a name="p157012058162714"></a><a name="p157012058162714"></a>Storage path of the application internal data</p>
</td>
</tr> ## dataStorage.removeStorageFromCacheSync
</tbody>
</table> removeStorageFromCacheSync(path: string): void
- Example: Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
```
dataStorage.deleteStorageSync(path + '/mystore')
```
## dataStorage.deleteStorage<a name="section17967123883712"></a>
deleteStorage\(path: string, callback: AsyncCallback<void\>\)
Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses an asynchronous callback to return the execution result.
- Parameters
<a name="table1296893843713"></a>
<table><thead align="left"><tr id="row5968193863710"><th class="cellrowborder" valign="top" width="11.59434422233057%" id="mcps1.1.5.1.1"><p id="p99681338103714"><a name="p99681338103714"></a><a name="p99681338103714"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="25.67528035104826%" id="mcps1.1.5.1.2"><p id="p1968838123715"><a name="p1968838123715"></a><a name="p1968838123715"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="8.3373963920039%" id="mcps1.1.5.1.3"><p id="p14968123803720"><a name="p14968123803720"></a><a name="p14968123803720"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="54.39297903461725%" id="mcps1.1.5.1.4"><p id="p7968238143715"><a name="p7968238143715"></a><a name="p7968238143715"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row129691538133719"><td class="cellrowborder" valign="top" width="11.59434422233057%" headers="mcps1.1.5.1.1 "><p id="p14969438143718"><a name="p14969438143718"></a><a name="p14969438143718"></a>path</p>
</td>
<td class="cellrowborder" valign="top" width="25.67528035104826%" headers="mcps1.1.5.1.2 "><p id="p9969123873716"><a name="p9969123873716"></a><a name="p9969123873716"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="8.3373963920039%" headers="mcps1.1.5.1.3 "><p id="p159691438173711"><a name="p159691438173711"></a><a name="p159691438173711"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="54.39297903461725%" headers="mcps1.1.5.1.4 "><p id="p06171654601"><a name="p06171654601"></a><a name="p06171654601"></a>Storage path of the application internal data</p>
</td>
</tr>
<tr id="row9969173819374"><td class="cellrowborder" valign="top" width="11.59434422233057%" headers="mcps1.1.5.1.1 "><p id="p69691238153715"><a name="p69691238153715"></a><a name="p69691238153715"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="25.67528035104826%" headers="mcps1.1.5.1.2 "><p id="p149691380378"><a name="p149691380378"></a><a name="p149691380378"></a>AsyncCallback&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="8.3373963920039%" headers="mcps1.1.5.1.3 "><p id="p2969103843715"><a name="p2969103843715"></a><a name="p2969103843715"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="54.39297903461725%" headers="mcps1.1.5.1.4 "><p id="p396915388374"><a name="p396915388374"></a><a name="p396915388374"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
dataStorage.deleteStorage(path + '/mystore', function (err) {
if (err) {
console.info("Deleted failed with err: " + err)
return
}
console.info("Deleted successfully.")
})
```
## dataStorage.deleteStorage<a name="section1497163823719"></a>
deleteStorage\(path: string\): Promise<void\>
Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses a promise to return the execution result.
- Parameters
<a name="table597293815378"></a>
<table><thead align="left"><tr id="row29721938203716"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p19972173883714"><a name="p19972173883714"></a><a name="p19972173883714"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p49721938103718"><a name="p49721938103718"></a><a name="p49721938103718"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p17972113863711"><a name="p17972113863711"></a><a name="p17972113863711"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p15972838123714"><a name="p15972838123714"></a><a name="p15972838123714"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1972163818374"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p597373833717"><a name="p597373833717"></a><a name="p597373833717"></a>path</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p11973173814378"><a name="p11973173814378"></a><a name="p11973173814378"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p209732388379"><a name="p209732388379"></a><a name="p209732388379"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p7812982288"><a name="p7812982288"></a><a name="p7812982288"></a>Storage path of the application internal data</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table5973738153710"></a>
<table><thead align="left"><tr id="row17973238123713"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p17973538183719"><a name="p17973538183719"></a><a name="p17973538183719"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p109731385374"><a name="p109731385374"></a><a name="p109731385374"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row89731838173711"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p34467362719"><a name="p34467362719"></a><a name="p34467362719"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p15446536474"><a name="p15446536474"></a><a name="p15446536474"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = dataStorage.deleteStorage(path + '/mystore')
promise.then(() => {
console.info("Deleted successfully.")
}).catch((err) => {
console.info("Deleted failed with err: " + err)
})
```
## dataStorage.removeStorageFromCacheSync<a name="section1212418572425"></a>
removeStorageFromCacheSync\(path: string\): void
Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
This method uses a synchronous mode. This method uses a synchronous mode.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table121245571424"></a> - Parameters
<table><thead align="left"><tr id="row61241257164218"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p13125165744216"><a name="p13125165744216"></a><a name="p13125165744216"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p141259575429"><a name="p141259575429"></a><a name="p141259575429"></a>Type</p> | path | string | Yes| Path of the target file.|
</th>
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p1312515724213"><a name="p1312515724213"></a><a name="p1312515724213"></a>Mandatory</p> - Example
</th> ```
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p6125157124212"><a name="p6125157124212"></a><a name="p6125157124212"></a>Description</p> dataStorage.removeStorageFromCacheSync(path + '/mystore')
</th> ```
</tr>
</thead>
<tbody><tr id="row1112595715421"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p1712610572429"><a name="p1712610572429"></a><a name="p1712610572429"></a>path</p> ## dataStorage.removeStorageFromCache
</td>
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p5126457164217"><a name="p5126457164217"></a><a name="p5126457164217"></a>string</p> removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void
</td>
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p19126457134212"><a name="p19126457134212"></a><a name="p19126457134212"></a>Yes</p> Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1724911392818"><a name="p1724911392818"></a><a name="p1724911392818"></a>Storage path of the application internal data</p> This method uses an asynchronous callback to return the result.
</td>
</tr> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</tbody>
</table> - Parameters
| Name| Type| Mandatory| Description|
- Example: | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.|
``` | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
dataStorage.removeStorageFromCacheSync(path + '/mystore')
``` - Example
```
dataStorage.removeStorageFromCache(path + '/mystore', function (err) {
## dataStorage.removeStorageFromCache<a name="section141288570429"></a> if (err) {
console.info("Removed storage from cache failed with err: " + err)
removeStorageFromCache\(path: string, callback: AsyncCallback<Storage\>\): void return
}
Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. console.info("Removed storage from cache successfully.")
})
This method uses an asynchronous mode. ```
- Parameters
## dataStorage.removeStorageFromCache
<a name="table12128115713423"></a>
<table><thead align="left"><tr id="row131291857124212"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p181291157174220"><a name="p181291157174220"></a><a name="p181291157174220"></a>Name</p> removeStorageFromCache(path: string): Promise&lt;void&gt;
</th>
<th class="cellrowborder" valign="top" width="25.29497805948318%" id="mcps1.1.5.1.2"><p id="p20129205715426"><a name="p20129205715426"></a><a name="p20129205715426"></a>Type</p> Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
</th>
<th class="cellrowborder" valign="top" width="7.664553876157972%" id="mcps1.1.5.1.3"><p id="p21294574428"><a name="p21294574428"></a><a name="p21294574428"></a>Mandatory</p> This method uses an asynchronous callback to return the result.
</th>
<th class="cellrowborder" valign="top" width="52.58898098488542%" id="mcps1.1.5.1.4"><p id="p512965716425"><a name="p512965716425"></a><a name="p512965716425"></a>Description</p> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</th>
</tr> - Parameters
</thead> | Name| Type| Mandatory| Description|
<tbody><tr id="row21299574423"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p17129105714421"><a name="p17129105714421"></a><a name="p17129105714421"></a>path</p> | -------- | -------- | -------- | -------- |
</td> | path | string | Yes| Path of the target file.|
<td class="cellrowborder" valign="top" width="25.29497805948318%" headers="mcps1.1.5.1.2 "><p id="p7129185794217"><a name="p7129185794217"></a><a name="p7129185794217"></a>string</p>
</td> - Return value
<td class="cellrowborder" valign="top" width="7.664553876157972%" headers="mcps1.1.5.1.3 "><p id="p813075717426"><a name="p813075717426"></a><a name="p813075717426"></a>Yes</p> | Type| Description|
</td> | -------- | -------- |
<td class="cellrowborder" valign="top" width="52.58898098488542%" headers="mcps1.1.5.1.4 "><p id="p3442141014113"><a name="p3442141014113"></a><a name="p3442141014113"></a>Storage path of the application internal data</p> | Promise&lt;void&gt; | Promise used to return the result.|
</td>
</tr> - Example
<tr id="row1813015717423"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p813085711421"><a name="p813085711421"></a><a name="p813085711421"></a>callback</p> ```
</td> let promiserevSt = dataStorage.removeStorageFromCache(path + '/mystore')
<td class="cellrowborder" valign="top" width="25.29497805948318%" headers="mcps1.1.5.1.2 "><p id="p013085719427"><a name="p013085719427"></a><a name="p013085719427"></a>AsyncCallback&lt;<a href="#section12882825611">Storage</a>&gt;</p> promiserevSt.then(() => {
</td> console.info("Removed storage from cache successfully.")
<td class="cellrowborder" valign="top" width="7.664553876157972%" headers="mcps1.1.5.1.3 "><p id="p181301457184219"><a name="p181301457184219"></a><a name="p181301457184219"></a>Yes</p> }).catch((err) => {
</td> console.info("Removed storage from cache failed with err: " + err)
<td class="cellrowborder" valign="top" width="52.58898098488542%" headers="mcps1.1.5.1.4 "><p id="p191301575425"><a name="p191301575425"></a><a name="p191301575425"></a>Callback used to return the result.</p> })
</td> ```
</tr>
</tbody>
</table> ## Storage
- Example:
```
dataStorage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) {
console.info("Removed storage from cache failed with err: " + err)
return
}
console.info("Removed storage from cache successfully.")
})
```
## dataStorage.removeStorageFromCache<a name="section1813245718422"></a>
removeStorageFromCache\(path: string\): Promise<void\>
Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
This method uses an asynchronous mode.
- Parameters
<a name="table2133125713429"></a>
<table><thead align="left"><tr id="row9133857194218"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p813355716423"><a name="p813355716423"></a><a name="p813355716423"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p213495712420"><a name="p213495712420"></a><a name="p213495712420"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p713465724210"><a name="p713465724210"></a><a name="p713465724210"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p413425734218"><a name="p413425734218"></a><a name="p413425734218"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row0134105716421"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p14134135744212"><a name="p14134135744212"></a><a name="p14134135744212"></a>path</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p613417578427"><a name="p613417578427"></a><a name="p613417578427"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p313555724217"><a name="p313555724217"></a><a name="p313555724217"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p88981021172812"><a name="p88981021172812"></a><a name="p88981021172812"></a>Storage path of the application internal data</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table513585718424"></a>
<table><thead align="left"><tr id="row191352576429"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p51351257104220"><a name="p51351257104220"></a><a name="p51351257104220"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p113611571421"><a name="p113611571421"></a><a name="p113611571421"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row0136165744213"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p159416454718"><a name="p159416454718"></a><a name="p159416454718"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p13941645472"><a name="p13941645472"></a><a name="p13941645472"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = dataStorage.removeStorageFromCache(path + '/mystore')
promise.then(() => {
console.info("Removed storage from cache successfully.")
}).catch((err) => {
console.info("Removed storage from cache failed with err: " + err)
})
```
## Storage<a name="section12882825611"></a>
Provides APIs for obtaining and modifying storage data. Provides APIs for obtaining and modifying storage data.
### getSync<a name="section1984422131910"></a>
getSync\(key: string, defValue: ValueType\): ValueType ### getSync
getSync(key: string, defValue: ValueType): ValueType
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.
This method uses a synchronous mode. This method uses a synchronous mode.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table134047259197"></a> - Parameters
<table><thead align="left"><tr id="row134041225191915"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p7404172513199"><a name="p7404172513199"></a><a name="p7404172513199"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p1740422518197"><a name="p1740422518197"></a><a name="p1740422518197"></a>Type</p> | key | string | Yes| Key of the data. It cannot be empty.|
</th> | defValue | ValueType | Yes| Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value.|
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p18404122513192"><a name="p18404122513192"></a><a name="p18404122513192"></a>Mandatory</p>
</th> - Return value
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p2404025121912"><a name="p2404025121912"></a><a name="p2404025121912"></a>Description</p> | Type| Description|
</th> | -------- | -------- |
</tr> | ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.|
</thead>
<tbody><tr id="row20404172517195"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p3404625101912"><a name="p3404625101912"></a><a name="p3404625101912"></a>key</p> - Example
</td> ```
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p114047255192"><a name="p114047255192"></a><a name="p114047255192"></a>string</p> let value = storage.getSync('startup', 'default')
</td> console.info("The value of startup is " + value)
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p10404162571912"><a name="p10404162571912"></a><a name="p10404162571912"></a>Yes</p> ```
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p10404192513199"><a name="p10404192513199"></a><a name="p10404192513199"></a>Key of the data. It cannot be empty.</p>
</td> ### get
</tr>
<tr id="row1740492512196"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p1540432551910"><a name="p1540432551910"></a><a name="p1540432551910"></a>defValue</p> get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void
</td>
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p8404102516199"><a name="p8404102516199"></a><a name="p8404102516199"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p6405202516196"><a name="p6405202516196"></a><a name="p6405202516196"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1140502514190"><a name="p1140502514190"></a><a name="p1140502514190"></a>Default value to be returned if the value of the specified key does not exist. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table432284131914"></a>
<table><thead align="left"><tr id="row14322941161913"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p83221416199"><a name="p83221416199"></a><a name="p83221416199"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p732219418192"><a name="p732219418192"></a><a name="p732219418192"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row63221941101918"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p932204131910"><a name="p932204131910"></a><a name="p932204131910"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p1832294181917"><a name="p1832294181917"></a><a name="p1832294181917"></a>Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let value = storage.getSync('startup', 'default')
console.info("The value of startup is " + value)
```
### get<a name="section12293161972016"></a>
get\(key: string, defValue: ValueType, callback: AsyncCallback<ValueType\>\): void
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.
This method uses an asynchronous mode. This method uses an asynchronous callback to return the result.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table4937172922016"></a> - Parameters
<table><thead align="left"><tr id="row3937162917202"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p39377296202"><a name="p39377296202"></a><a name="p39377296202"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p1093712917202"><a name="p1093712917202"></a><a name="p1093712917202"></a>Type</p> | key | string | Yes| Key of the data. It cannot be empty.|
</th> | defValue | ValueType | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p5937142915201"><a name="p5937142915201"></a><a name="p5937142915201"></a>Mandatory</p> | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p693722972018"><a name="p693722972018"></a><a name="p693722972018"></a>Description</p> - Example
</th> ```
</tr> storage.get('startup', 'default', function(err, value) {
</thead> if (err) {
<tbody><tr id="row0937829202013"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1993718293203"><a name="p1993718293203"></a><a name="p1993718293203"></a>key</p> console.info("Get the value of startup failed with err: " + err)
</td> return
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p1937122922018"><a name="p1937122922018"></a><a name="p1937122922018"></a>string</p> }
</td> console.info("The value of startup is " + value)
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p10937152962019"><a name="p10937152962019"></a><a name="p10937152962019"></a>Yes</p> })
</td> ```
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p793710294206"><a name="p793710294206"></a><a name="p793710294206"></a>Key of the data. It cannot be empty.</p>
</td>
</tr> ### get
<tr id="row1293752912012"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1893714292206"><a name="p1893714292206"></a><a name="p1893714292206"></a>defValue</p>
</td> get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p119371729132012"><a name="p119371729132012"></a><a name="p119371729132012"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p12937429102016"><a name="p12937429102016"></a><a name="p12937429102016"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p129372299204"><a name="p129372299204"></a><a name="p129372299204"></a>Default value to be returned. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
<tr id="row109381229142014"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1693815299207"><a name="p1693815299207"></a><a name="p1693815299207"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p15938329142010"><a name="p15938329142010"></a><a name="p15938329142010"></a>AsyncCallback&lt;ValueType&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p10938929162017"><a name="p10938929162017"></a><a name="p10938929162017"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p893812912201"><a name="p893812912201"></a><a name="p893812912201"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
storage.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<a name="section18984643152118"></a>
get\(key: string, defValue: ValueType\): Promise<ValueType\>
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned.
This method uses an asynchronous mode. This method uses an asynchronous callback to return the result.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table18177853162110"></a> - Parameters
<table><thead align="left"><tr id="row181781453102111"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p13178953122112"><a name="p13178953122112"></a><a name="p13178953122112"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p111781153172115"><a name="p111781153172115"></a><a name="p111781153172115"></a>Type</p> | key | string | Yes| Key of the data. It cannot be empty.|
</th> | defValue | ValueType | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p5178125342118"><a name="p5178125342118"></a><a name="p5178125342118"></a>Mandatory</p>
</th> - Return value
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p17178105320210"><a name="p17178105320210"></a><a name="p17178105320210"></a>Description</p> | Type| Description|
</th> | -------- | -------- |
</tr> | Promise&lt;ValueType&gt; | Promise used to return the result.|
</thead>
<tbody><tr id="row81781153162116"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p91786538215"><a name="p91786538215"></a><a name="p91786538215"></a>key</p> - Example
</td> ```
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p11178105316219"><a name="p11178105316219"></a><a name="p11178105316219"></a>string</p> let promiseget = storage.get('startup', 'default')
</td> promiseget.then((value) => {
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p71781653142115"><a name="p71781653142115"></a><a name="p71781653142115"></a>Yes</p> console.info("The value of startup is " + value)
</td> }).catch((err) => {
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p717812534216"><a name="p717812534216"></a><a name="p717812534216"></a>Key of the data. It cannot be empty.</p> console.info("Get the value of startup failed with err: " + err)
</td> })
</tr> ```
<tr id="row191781653182116"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p7178185315217"><a name="p7178185315217"></a><a name="p7178185315217"></a>defValue</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p6178105319217"><a name="p6178105319217"></a><a name="p6178105319217"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p17178653132119"><a name="p17178653132119"></a><a name="p17178653132119"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p817813533210"><a name="p817813533210"></a><a name="p817813533210"></a>Default value to be returned. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table17633151592210"></a>
<table><thead align="left"><tr id="row12633615192219"><th class="cellrowborder" valign="top" width="29.59%" id="mcps1.1.3.1.1"><p id="p15633161517226"><a name="p15633161517226"></a><a name="p15633161517226"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="70.41%" id="mcps1.1.3.1.2"><p id="p106331915142212"><a name="p106331915142212"></a><a name="p106331915142212"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row14633115142215"><td class="cellrowborder" valign="top" width="29.59%" headers="mcps1.1.3.1.1 "><p id="p56331015142214"><a name="p56331015142214"></a><a name="p56331015142214"></a>Promise&lt;ValueType&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="70.41%" headers="mcps1.1.3.1.2 "><p id="p15633101522214"><a name="p15633101522214"></a><a name="p15633101522214"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = storage.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)
})
```
### putSync<a name="section2042914810266"></a>
putSync\(key: string, value: ValueType\): void
Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**.
This method uses a synchronous mode.
- Parameters ### putSync
<a name="table18627821182614"></a> putSync(key: string, value: ValueType): void
<table><thead align="left"><tr id="row162782182615"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p36278211263"><a name="p36278211263"></a><a name="p36278211263"></a>Name</p>
</th> Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**.
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p1662732192616"><a name="p1662732192616"></a><a name="p1662732192616"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p1362762182616"><a name="p1362762182616"></a><a name="p1362762182616"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p186282218261"><a name="p186282218261"></a><a name="p186282218261"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1262810215268"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p196284210266"><a name="p196284210266"></a><a name="p196284210266"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p1462802113268"><a name="p1462802113268"></a><a name="p1462802113268"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p1628192192612"><a name="p1628192192612"></a><a name="p1628192192612"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p6628521122615"><a name="p6628521122615"></a><a name="p6628521122615"></a>Key of the data to be modified. It cannot be empty.</p>
</td>
</tr>
<tr id="row5628202152619"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p8628102192617"><a name="p8628102192617"></a><a name="p8628102192617"></a>value</p>
</td>
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p66281421192620"><a name="p66281421192620"></a><a name="p66281421192620"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p96281217267"><a name="p96281217267"></a><a name="p96281217267"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p15628721142612"><a name="p15628721142612"></a><a name="p15628721142612"></a>New value. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
storage.putSync('startup', 'auto')
```
### put<a name="section141693514276"></a>
put\(key: string, value: ValueType, callback: AsyncCallback<void\>\): void
Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**.
This method uses an asynchronous mode.
- Parameters
<a name="table875524602811"></a>
<table><thead align="left"><tr id="row2075518465285"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p1075574672815"><a name="p1075574672815"></a><a name="p1075574672815"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="23.539736713798145%" id="mcps1.1.5.1.2"><p id="p6755114618283"><a name="p6755114618283"></a><a name="p6755114618283"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="10.4534373476353%" id="mcps1.1.5.1.3"><p id="p137551446142814"><a name="p137551446142814"></a><a name="p137551446142814"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="51.55533885909311%" id="mcps1.1.5.1.4"><p id="p1275544652813"><a name="p1275544652813"></a><a name="p1275544652813"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row7755124692813"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p17755346102818"><a name="p17755346102818"></a><a name="p17755346102818"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="23.539736713798145%" headers="mcps1.1.5.1.2 "><p id="p177551846172817"><a name="p177551846172817"></a><a name="p177551846172817"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="10.4534373476353%" headers="mcps1.1.5.1.3 "><p id="p16756946182810"><a name="p16756946182810"></a><a name="p16756946182810"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="51.55533885909311%" headers="mcps1.1.5.1.4 "><p id="p775614662815"><a name="p775614662815"></a><a name="p775614662815"></a>Key of the data to be modified. It cannot be empty.</p>
</td>
</tr>
<tr id="row1875674613284"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1175634652819"><a name="p1175634652819"></a><a name="p1175634652819"></a>value</p>
</td>
<td class="cellrowborder" valign="top" width="23.539736713798145%" headers="mcps1.1.5.1.2 "><p id="p37561846152814"><a name="p37561846152814"></a><a name="p37561846152814"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="10.4534373476353%" headers="mcps1.1.5.1.3 "><p id="p075684619281"><a name="p075684619281"></a><a name="p075684619281"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="51.55533885909311%" headers="mcps1.1.5.1.4 "><p id="p117561246192811"><a name="p117561246192811"></a><a name="p117561246192811"></a>New value. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
<tr id="row375674614288"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p14756134672812"><a name="p14756134672812"></a><a name="p14756134672812"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="23.539736713798145%" headers="mcps1.1.5.1.2 "><p id="p1075614612285"><a name="p1075614612285"></a><a name="p1075614612285"></a>AsyncCallback&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="10.4534373476353%" headers="mcps1.1.5.1.3 "><p id="p2756746202817"><a name="p2756746202817"></a><a name="p2756746202817"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="51.55533885909311%" headers="mcps1.1.5.1.4 "><p id="p375610463287"><a name="p375610463287"></a><a name="p375610463287"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
storage.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<a name="section594485413298"></a>
put\(key: string, value: ValueType\): Promise<void\>
Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**.
This method uses an asynchronous mode.
- Parameters
<a name="table144341091305"></a>
<table><thead align="left"><tr id="row7434691304"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p1343429163015"><a name="p1343429163015"></a><a name="p1343429163015"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p04349953017"><a name="p04349953017"></a><a name="p04349953017"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p3434395307"><a name="p3434395307"></a><a name="p3434395307"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p154349963020"><a name="p154349963020"></a><a name="p154349963020"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row16434196302"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p14345943014"><a name="p14345943014"></a><a name="p14345943014"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p8434159163014"><a name="p8434159163014"></a><a name="p8434159163014"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p124341963010"><a name="p124341963010"></a><a name="p124341963010"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p144354963019"><a name="p144354963019"></a><a name="p144354963019"></a>Key of the data to be modified. It cannot be empty.</p>
</td>
</tr>
<tr id="row18435109123019"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p194354913309"><a name="p194354913309"></a><a name="p194354913309"></a>value</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p24358918307"><a name="p24358918307"></a><a name="p24358918307"></a>ValueType</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p5435994306"><a name="p5435994306"></a><a name="p5435994306"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p17435149123016"><a name="p17435149123016"></a><a name="p17435149123016"></a>New value. The value can be a number, string, or Boolean value.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table3877202333010"></a>
<table><thead align="left"><tr id="row1687722319306"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p187722311304"><a name="p187722311304"></a><a name="p187722311304"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p18772239307"><a name="p18772239307"></a><a name="p18772239307"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1287820238301"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p987892315302"><a name="p987892315302"></a><a name="p987892315302"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p208781523143017"><a name="p208781523143017"></a><a name="p208781523143017"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = storage.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)
})
```
### hasSync<a name="section7273343143018"></a>
hasSync\(key: string\): boolean
Checks whether the storage object contains data with the specified key.
This method uses a synchronous mode. This method uses a synchronous mode.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table206971230310"></a> - Parameters
<table><thead align="left"><tr id="row13697434313"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p1169763133112"><a name="p1169763133112"></a><a name="p1169763133112"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p669716343112"><a name="p669716343112"></a><a name="p669716343112"></a>Type</p> | key | string | Yes| Key of the data to modify. It cannot be empty.|
</th> | value | ValueType | Yes| New value to store. It can be a number, string, or Boolean value.|
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p1869717312318"><a name="p1869717312318"></a><a name="p1869717312318"></a>Mandatory</p>
</th> - Example
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p146971031315"><a name="p146971031315"></a><a name="p146971031315"></a>Description</p> ```
</th> storage.putSync('startup', 'auto')
</tr> ```
</thead>
<tbody><tr id="row169719323119"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p1269713323114"><a name="p1269713323114"></a><a name="p1269713323114"></a>key</p>
</td> ### put
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p1697193193119"><a name="p1697193193119"></a><a name="p1697193193119"></a>string</p>
</td> put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p1669716313310"><a name="p1669716313310"></a><a name="p1669716313310"></a>Yes</p>
</td> Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**.
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p8697139312"><a name="p8697139312"></a><a name="p8697139312"></a>Key of the data. It cannot be empty.</p>
</td> This method uses an asynchronous callback to return the result.
</tr>
</tbody> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</table>
- Parameters
- Return values | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
<a name="table269819353119"></a> | key | string | Yes| Key of the data to modify. It cannot be empty.|
<table><thead align="left"><tr id="row10698153173115"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p46983317313"><a name="p46983317313"></a><a name="p46983317313"></a>Type</p> | value | ValueType | Yes| New value to store. It can be a number, string, or Boolean value.|
</th> | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p769815310315"><a name="p769815310315"></a><a name="p769815310315"></a>Description</p>
</th> - Example
</tr> ```
</thead> storage.put('startup', 'auto', function (err) {
<tbody><tr id="row1769815310313"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p869833203110"><a name="p869833203110"></a><a name="p869833203110"></a>boolean</p> if (err) {
</td> console.info("Put the value of startup failed with err: " + err)
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p166981339312"><a name="p166981339312"></a><a name="p166981339312"></a>Returns <strong id="b35918155715"><a name="b35918155715"></a><a name="b35918155715"></a>true</strong> if the storage object contains data with the specified key; returns <strong id="b105911713577"><a name="b105911713577"></a><a name="b105911713577"></a>false</strong> otherwise.</p> return
</td> }
</tr> console.info("Put the value of startup successfully.")
</tbody> })
</table> ```
- Example:
### put
```
let isExist = storage.hasSync('startup') put(key: string, value: ValueType): Promise&lt;void&gt;
if (isExist) {
console.info("The key of startup is contained.") Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**.
}
``` This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
### has<a name="section185676222325"></a>
- Parameters
has\(key: string, callback: AsyncCallback<boolean\>\): boolean | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
Checks whether the storage object contains data with the specified key. | key | string | Yes| Key of the data to modify. It cannot be empty.|
| value | ValueType | Yes| New value to store. It can be a number, string, or Boolean value.|
This method uses an asynchronous mode.
- Return value
- Parameters | Type| Description|
| -------- | -------- |
<a name="table1769918466400"></a> | Promise&lt;void&gt; | Promise used to return the result.|
<table><thead align="left"><tr id="row19699194616406"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p76991946164012"><a name="p76991946164012"></a><a name="p76991946164012"></a>Name</p>
</th> - Example
<th class="cellrowborder" valign="top" width="26.738176499268647%" id="mcps1.1.5.1.2"><p id="p669954618400"><a name="p669954618400"></a><a name="p669954618400"></a>Type</p> ```
</th> let promiseput = storage.put('startup', 'auto')
<th class="cellrowborder" valign="top" width="10.68746952705997%" id="mcps1.1.5.1.3"><p id="p136994463402"><a name="p136994463402"></a><a name="p136994463402"></a>Mandatory</p> promiseput.then(() => {
</th> console.info("Put the value of startup successfully.")
<th class="cellrowborder" valign="top" width="48.12286689419795%" id="mcps1.1.5.1.4"><p id="p1069984634014"><a name="p1069984634014"></a><a name="p1069984634014"></a>Description</p> }).catch((err) => {
</th> console.info("Put the value of startup failed with err: " + err)
</tr> })
</thead> ```
<tbody><tr id="row17699646184013"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1769917466409"><a name="p1769917466409"></a><a name="p1769917466409"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="26.738176499268647%" headers="mcps1.1.5.1.2 "><p id="p3699124614409"><a name="p3699124614409"></a><a name="p3699124614409"></a>string</p> ### hasSync
</td>
<td class="cellrowborder" valign="top" width="10.68746952705997%" headers="mcps1.1.5.1.3 "><p id="p176992461406"><a name="p176992461406"></a><a name="p176992461406"></a>Yes</p> hasSync(key: string): boolean
</td>
<td class="cellrowborder" valign="top" width="48.12286689419795%" headers="mcps1.1.5.1.4 "><p id="p14699746114020"><a name="p14699746114020"></a><a name="p14699746114020"></a>Key of the data. It cannot be empty.</p> Checks whether the storage object contains data with a given key.
</td>
</tr>
<tr id="row10699164684011"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p166994467401"><a name="p166994467401"></a><a name="p166994467401"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="26.738176499268647%" headers="mcps1.1.5.1.2 "><p id="p3699746194019"><a name="p3699746194019"></a><a name="p3699746194019"></a>AsyncCallback&lt;boolean&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="10.68746952705997%" headers="mcps1.1.5.1.3 "><p id="p8700746114012"><a name="p8700746114012"></a><a name="p8700746114012"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="48.12286689419795%" headers="mcps1.1.5.1.4 "><p id="p147004466406"><a name="p147004466406"></a><a name="p147004466406"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table1150162734110"></a>
<table><thead align="left"><tr id="row1950110276414"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p75011227194112"><a name="p75011227194112"></a><a name="p75011227194112"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p1550192794116"><a name="p1550192794116"></a><a name="p1550192794116"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row9501627134110"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p11501112734115"><a name="p11501112734115"></a><a name="p11501112734115"></a>boolean</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p1350212774110"><a name="p1350212774110"></a><a name="p1350212774110"></a>Returns <strong id="b1242063601510"><a name="b1242063601510"></a><a name="b1242063601510"></a>true</strong> if the storage object contains data with the specified key; returns <strong id="b1142033612155"><a name="b1142033612155"></a><a name="b1142033612155"></a>false</strong> otherwise.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
storage.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<a name="section415084610412"></a>
has\(key: string\): Promise<boolean\>
Checks whether the storage object contains data with the specified key.
This method uses an asynchronous mode.
- Parameters
<a name="table10418320457"></a>
<table><thead align="left"><tr id="row15414329454"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p6423294518"><a name="p6423294518"></a><a name="p6423294518"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p11443274517"><a name="p11443274517"></a><a name="p11443274517"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p104132164511"><a name="p104132164511"></a><a name="p104132164511"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p5423224514"><a name="p5423224514"></a><a name="p5423224514"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row34732194515"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p641232154519"><a name="p641232154519"></a><a name="p641232154519"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p2047321454"><a name="p2047321454"></a><a name="p2047321454"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p64203213458"><a name="p64203213458"></a><a name="p64203213458"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p1433213456"><a name="p1433213456"></a><a name="p1433213456"></a>Key of the data. It cannot be empty.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table1120619131462"></a>
<table><thead align="left"><tr id="row12063139466"><th class="cellrowborder" valign="top" width="30.56%" id="mcps1.1.3.1.1"><p id="p15206213154614"><a name="p15206213154614"></a><a name="p15206213154614"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="69.44%" id="mcps1.1.3.1.2"><p id="p1920651317466"><a name="p1920651317466"></a><a name="p1920651317466"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1620731334615"><td class="cellrowborder" valign="top" width="30.56%" headers="mcps1.1.3.1.1 "><p id="p9207161324612"><a name="p9207161324612"></a><a name="p9207161324612"></a>Promise&lt;boolean&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="69.44%" headers="mcps1.1.3.1.2 "><p id="p202076136463"><a name="p202076136463"></a><a name="p202076136463"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = storage.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)
})
```
### deleteSync<a name="section31481447174613"></a>
deleteSync\(key: string\): void
Deletes the data with the specified key from this storage object.
This method uses a synchronous mode. This method uses a synchronous mode.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table733743144711"></a> - Parameters
<table><thead align="left"><tr id="row233733114710"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p18337137471"><a name="p18337137471"></a><a name="p18337137471"></a>Name</p> | Name| Type| Mandatory| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p23378324711"><a name="p23378324711"></a><a name="p23378324711"></a>Type</p> | key | string | Yes| Key of the data. It cannot be empty.|
</th>
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p53377324710"><a name="p53377324710"></a><a name="p53377324710"></a>Mandatory</p> - Return value
</th> | Type| Description|
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p73371133477"><a name="p73371133477"></a><a name="p73371133477"></a>Description</p> | -------- | -------- |
</th> | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
</tr>
</thead> - Example
<tbody><tr id="row733793124714"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p123378374715"><a name="p123378374715"></a><a name="p123378374715"></a>key</p> ```
</td> let isExist = storage.hasSync('startup')
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p16338836476"><a name="p16338836476"></a><a name="p16338836476"></a>string</p> if (isExist) {
</td> console.info("The key of startup is contained.")
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p12338193194714"><a name="p12338193194714"></a><a name="p12338193194714"></a>Yes</p> }
</td> ```
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p9338731476"><a name="p9338731476"></a><a name="p9338731476"></a>Key of the data. It cannot be empty.</p>
</td>
</tr> ### has
</tbody>
</table> has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean
- Example: Checks whether the storage object contains data with a given key.
``` This method uses an asynchronous callback to return the result.
storage.deleteSync('startup')
``` **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
### delete<a name="section31755184812"></a> | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
delete\(key: string, callback: AsyncCallback<void\>\): void | key | string | Yes| Key of the data. It cannot be empty.|
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|
Deletes the data with the specified key from this storage object.
- Return value
This method uses an asynchronous mode. | Type| Description|
| -------- | -------- |
- Parameters | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
<a name="table1463510520490"></a> - Example
<table><thead align="left"><tr id="row116351594920"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p1963515554910"><a name="p1963515554910"></a><a name="p1963515554910"></a>Name</p> ```
</th> storage.has('startup', function (err, isExist) {
<th class="cellrowborder" valign="top" width="24.48561677230619%" id="mcps1.1.5.1.2"><p id="p1263535104918"><a name="p1263535104918"></a><a name="p1263535104918"></a>Type</p> if (err) {
</th> console.info("Check the key of startup failed with err: " + err)
<th class="cellrowborder" valign="top" width="9.985372988785958%" id="mcps1.1.5.1.3"><p id="p063513511492"><a name="p063513511492"></a><a name="p063513511492"></a>Mandatory</p> return
</th> }
<th class="cellrowborder" valign="top" width="51.07752315943442%" id="mcps1.1.5.1.4"><p id="p463555174910"><a name="p463555174910"></a><a name="p463555174910"></a>Description</p> if (isExist) {
</th> console.info("The key of startup is contained.")
</tr> }
</thead> })
<tbody><tr id="row13636175124918"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1663635174919"><a name="p1663635174919"></a><a name="p1663635174919"></a>key</p> ```
</td>
<td class="cellrowborder" valign="top" width="24.48561677230619%" headers="mcps1.1.5.1.2 "><p id="p12636145184919"><a name="p12636145184919"></a><a name="p12636145184919"></a>string</p>
</td> ### has
<td class="cellrowborder" valign="top" width="9.985372988785958%" headers="mcps1.1.5.1.3 "><p id="p76361652495"><a name="p76361652495"></a><a name="p76361652495"></a>Yes</p>
</td> has(key: string): Promise&lt;boolean&gt;
<td class="cellrowborder" valign="top" width="51.07752315943442%" headers="mcps1.1.5.1.4 "><p id="p76361658497"><a name="p76361658497"></a><a name="p76361658497"></a>Key of the data. It cannot be empty.</p>
</td> Checks whether the storage object contains data with a given key.
</tr>
<tr id="row96363574913"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p763616510498"><a name="p763616510498"></a><a name="p763616510498"></a>callback</p> This method uses an asynchronous callback to return the result.
</td>
<td class="cellrowborder" valign="top" width="24.48561677230619%" headers="mcps1.1.5.1.2 "><p id="p1636125174918"><a name="p1636125174918"></a><a name="p1636125174918"></a>AsyncCallback&lt;void&gt;</p> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
</td>
<td class="cellrowborder" valign="top" width="9.985372988785958%" headers="mcps1.1.5.1.3 "><p id="p463615517491"><a name="p463615517491"></a><a name="p463615517491"></a>Yes</p> - Parameters
</td> | Name| Type| Mandatory| Description|
<td class="cellrowborder" valign="top" width="51.07752315943442%" headers="mcps1.1.5.1.4 "><p id="p156361259495"><a name="p156361259495"></a><a name="p156361259495"></a>Callback used to return the result.</p> | -------- | -------- | -------- | -------- |
</td> | key | string | Yes| Key of the data. It cannot be empty.|
</tr>
</tbody> - Return value
</table> | Type| Description|
| -------- | -------- |
- Example: | Promise&lt;boolean&gt; | Promise used to return the result.|
``` - Example
storage.delete('startup', function (err) { ```
if (err) { let promisehas = storage.has('startup')
console.info("Delete startup key failed with err: " + err) promisehas.then((isExist) => {
return if (isExist) {
} console.info("The key of startup is contained.")
console.info("Deleted startup key successfully.") }
}) }).catch((err) => {
``` console.info("Check the key of startup failed with err: " + err)
})
```
### delete<a name="section11711858125016"></a>
delete\(key: string\): Promise<void\> ### deleteSync
Deletes the data with the specified key from this storage object. deleteSync(key: string): void
This method uses an asynchronous mode. Deletes data with the specified key from this storage object.
- Parameters
<a name="table193166815517"></a>
<table><thead align="left"><tr id="row16316784518"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p1331615865119"><a name="p1331615865119"></a><a name="p1331615865119"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="18.95660653339834%" id="mcps1.1.5.1.2"><p id="p9316178135110"><a name="p9316178135110"></a><a name="p9316178135110"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="6.825938566552901%" id="mcps1.1.5.1.3"><p id="p11316158115115"><a name="p11316158115115"></a><a name="p11316158115115"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.76596782057533%" id="mcps1.1.5.1.4"><p id="p1631613812519"><a name="p1631613812519"></a><a name="p1631613812519"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1231618811515"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p1831613813513"><a name="p1831613813513"></a><a name="p1831613813513"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="18.95660653339834%" headers="mcps1.1.5.1.2 "><p id="p83162805112"><a name="p83162805112"></a><a name="p83162805112"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="6.825938566552901%" headers="mcps1.1.5.1.3 "><p id="p43161845116"><a name="p43161845116"></a><a name="p43161845116"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p183171819512"><a name="p183171819512"></a><a name="p183171819512"></a>Key of the data.</p>
</td>
</tr>
</tbody>
</table>
- Return values
<a name="table6698194765112"></a>
<table><thead align="left"><tr id="row36984478516"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p15698124745118"><a name="p15698124745118"></a><a name="p15698124745118"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p1269816474513"><a name="p1269816474513"></a><a name="p1269816474513"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row16698247205114"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p18698447105115"><a name="p18698447105115"></a><a name="p18698447105115"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p36981947155117"><a name="p36981947155117"></a><a name="p36981947155117"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = storage.delete('startup')
promise.then(() => {
console.info("Deleted startup key successfully.")
}).catch((err) => {
console.info("Delete startup key failed with err: " + err)
})
```
### flushSync<a name="section1952191618523"></a>
flushSync\(\): void
Saves the modification of the current object to the current **Storage** instance and synchronizes the modification to the file.
This method uses a synchronous mode. This method uses a synchronous mode.
- Example: **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
- Example
```
storage.deleteSync('startup')
```
### delete
delete(key: string, callback: AsyncCallback&lt;void&gt;): void
Deletes data with the specified key from this storage object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example
```
storage.delete('startup', function (err) {
if (err) {
console.info("Delete startup key failed with err: " + err)
return
}
console.info("Deleted startup key successfully.")
})
```
``` ### delete
storage.flushSync()
```
delete(key: string): Promise&lt;void&gt;
### flush<a name="section65806875414"></a> Deletes data with the specified key from this storage object.
flush\(callback: AsyncCallback<void\>\): void This method uses an asynchronous callback to return the result.
Saves the modification of the current object to the current **Storage** instance and stores the modification to the file in an asynchronous mode. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
This method uses an asynchronous mode. - Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data.|
- Parameters - Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
<a name="table158501215125418"></a> - Example
<table><thead align="left"><tr id="row9850415125415"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p17850115155417"><a name="p17850115155417"></a><a name="p17850115155417"></a>Name</p> ```
</th> let promisedel = storage.delete('startup')
<th class="cellrowborder" valign="top" width="25.041443198439783%" id="mcps1.1.5.1.2"><p id="p1585081595414"><a name="p1585081595414"></a><a name="p1585081595414"></a>Type</p> promisedel.then(() => {
</th> console.info("Deleted startup key successfully.")
<th class="cellrowborder" valign="top" width="7.372013651877133%" id="mcps1.1.5.1.3"><p id="p18509157544"><a name="p18509157544"></a><a name="p18509157544"></a>Mandatory</p> }).catch((err) => {
</th> console.info("Delete startup key failed with err: " + err)
<th class="cellrowborder" valign="top" width="53.13505607020965%" id="mcps1.1.5.1.4"><p id="p785020151545"><a name="p785020151545"></a><a name="p785020151545"></a>Description</p> })
</th> ```
</tr>
</thead>
<tbody><tr id="row198503159545"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p785118152548"><a name="p785118152548"></a><a name="p785118152548"></a>callback</p> ### flushSync
</td>
<td class="cellrowborder" valign="top" width="25.041443198439783%" headers="mcps1.1.5.1.2 "><p id="p48511715105417"><a name="p48511715105417"></a><a name="p48511715105417"></a>AsyncCallback&lt;void&gt;</p> flushSync(): void
</td>
<td class="cellrowborder" valign="top" width="7.372013651877133%" headers="mcps1.1.5.1.3 "><p id="p20851201513548"><a name="p20851201513548"></a><a name="p20851201513548"></a>Yes</p> Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
</td>
<td class="cellrowborder" valign="top" width="53.13505607020965%" headers="mcps1.1.5.1.4 "><p id="p19851715105412"><a name="p19851715105412"></a><a name="p19851715105412"></a>Callback used to return the result.</p> This method uses a synchronous mode.
</td>
</tr>
</tbody>
</table>
- Example: **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
``` - Example
storage.flush(function (err) { ```
if (err) { storage.flushSync()
console.info("Flush to file failed with err: " + err) ```
return
}
console.info("Flushed to file successfully.") ### flush
})
```
flush(callback: AsyncCallback&lt;void&gt;): void
### flush<a name="section524213130551"></a> Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
flush\(\): Promise<void\> This method uses an asynchronous callback to return the result.
Saves the modification of the current object to the current **Storage** instance and stores the modification to the file in an asynchronous mode. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
This method uses an asynchronous mode. - Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Return values - Example
```
storage.flush(function (err) {
if (err) {
console.info("Flush to file failed with err: " + err)
return
}
console.info("Flushed to file successfully.")
})
```
<a name="table197324403552"></a>
<table><thead align="left"><tr id="row197328403555"><th class="cellrowborder" valign="top" width="17.01%" id="mcps1.1.3.1.1"><p id="p1173344085511"><a name="p1173344085511"></a><a name="p1173344085511"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="82.99%" id="mcps1.1.3.1.2"><p id="p87336404552"><a name="p87336404552"></a><a name="p87336404552"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row573324045513"><td class="cellrowborder" valign="top" width="17.01%" headers="mcps1.1.3.1.1 "><p id="p773318409553"><a name="p773318409553"></a><a name="p773318409553"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p1273364018550"><a name="p1273364018550"></a><a name="p1273364018550"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example: ### flush
``` flush(): Promise&lt;void&gt;
let promise = storage.flush()
promise.then(() => {
console.info("Flushed to file successfully.")
}).catch((err) => {
console.info("Flush to file failed with err: " + err)
})
```
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
### clearSync<a name="section119861517115914"></a> This method uses an asynchronous callback to return the result.
clearSync\(\): void **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Clears all data in a storage object. - Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promiseflush = storage.flush()
promiseflush.then(() => {
console.info("Flushed to file successfully.")
}).catch((err) => {
console.info("Flush to file failed with err: " + err)
})
```
### clearSync
clearSync(): void
Clears this **Storage** object.
This method uses a synchronous mode. This method uses a synchronous mode.
- Example: **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Example
```
storage.clearSync()
```
### clear
clear(callback: AsyncCallback&lt;void&gt;): void
Clears this **Storage** object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
``` - Example
storage.clearSync() ```
``` storage.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;
Clears this **Storage** object.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Return value
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
```
let promiseclear = storage.clear()
promiseclear.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;StorageObserver&gt;): void
Subscribes to data changes. The **StorageObserver** needs to be implemented. When the value of the key subscribed to is changed, a callback will be invoked after **flush()** or **flushSync()** is executed.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
- Parameters
| Name| Type| Description|
| -------- | -------- | -------- |
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|
- Example
```
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
storage.on('change', observer)
storage.putSync('startup', 'auto')
storage.flushSync() // observer will be called.
```
### off('change')
off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
### clear<a name="section15671520306"></a>
clear\(callback: AsyncCallback<void\>\): void
Clears all data in a storage object.
This method uses an asynchronous mode.
- Parameters
<a name="table84588321621"></a>
<table><thead align="left"><tr id="row154589321724"><th class="cellrowborder" valign="top" width="14.451487079473427%" id="mcps1.1.5.1.1"><p id="p17458732925"><a name="p17458732925"></a><a name="p17458732925"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="22.837640175524133%" id="mcps1.1.5.1.2"><p id="p1945893212211"><a name="p1945893212211"></a><a name="p1945893212211"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="11.389566065333982%" id="mcps1.1.5.1.3"><p id="p24590321822"><a name="p24590321822"></a><a name="p24590321822"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="51.32130667966845%" id="mcps1.1.5.1.4"><p id="p194593321528"><a name="p194593321528"></a><a name="p194593321528"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row2459163212210"><td class="cellrowborder" valign="top" width="14.451487079473427%" headers="mcps1.1.5.1.1 "><p id="p11459832229"><a name="p11459832229"></a><a name="p11459832229"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="22.837640175524133%" headers="mcps1.1.5.1.2 "><p id="p1145913211215"><a name="p1145913211215"></a><a name="p1145913211215"></a>AsyncCallback&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="11.389566065333982%" headers="mcps1.1.5.1.3 "><p id="p13459183218216"><a name="p13459183218216"></a><a name="p13459183218216"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="51.32130667966845%" headers="mcps1.1.5.1.4 "><p id="p84590321224"><a name="p84590321224"></a><a name="p84590321224"></a>Callback used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
storage.clear(function (err) {
if (err) {
console.info("Clear to file failed with err: " + err)
return
}
console.info("Cleared to file successfully.")
})
```
### clear<a name="section21300181536"></a>
clear\(\): Promise<void\>
Clears all data in a storage object.
This method uses an asynchronous mode.
- Return values
<a name="table123728475316"></a>
<table><thead align="left"><tr id="row11372947131"><th class="cellrowborder" valign="top" width="20.380000000000003%" id="mcps1.1.3.1.1"><p id="p13372104710314"><a name="p13372104710314"></a><a name="p13372104710314"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="79.62%" id="mcps1.1.3.1.2"><p id="p1737316471834"><a name="p1737316471834"></a><a name="p1737316471834"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row337374719310"><td class="cellrowborder" valign="top" width="20.380000000000003%" headers="mcps1.1.3.1.1 "><p id="p1937313478315"><a name="p1937313478315"></a><a name="p1937313478315"></a>Promise&lt;void&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="79.62%" headers="mcps1.1.3.1.2 "><p id="p1337317471631"><a name="p1337317471631"></a><a name="p1337317471631"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
let promise = storage.clear()
promise.then(() => {
console.info("Cleared to file successfully.")
}).catch((err) => {
console.info("Clear to file failed with err: " + err)
})
```
### on \('change'\)<a name="section189332511954"></a>
on\(type: 'change', callback: Callback<StorageObserver\>\): void
Subscribes to data changes. The **StorageObserver** needs to be implemented. When the value of the key subscribed to is changed, a callback will be invoked after **flush\(\)** or **flushSync\(\)** is executed.
- Parameters
<a name="table61212334717"></a>
<table><thead align="left"><tr id="row6122133313720"><th class="cellrowborder" valign="top" width="17.119999999999997%" id="mcps1.1.4.1.1"><p id="p112215335718"><a name="p112215335718"></a><a name="p112215335718"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="32.190000000000005%" id="mcps1.1.4.1.2"><p id="p1912223317713"><a name="p1912223317713"></a><a name="p1912223317713"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="50.690000000000005%" id="mcps1.1.4.1.3"><p id="p912219331374"><a name="p912219331374"></a><a name="p912219331374"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1412293319714"><td class="cellrowborder" valign="top" width="17.119999999999997%" headers="mcps1.1.4.1.1 "><p id="p11122203314718"><a name="p11122203314718"></a><a name="p11122203314718"></a>type</p>
</td>
<td class="cellrowborder" valign="top" width="32.190000000000005%" headers="mcps1.1.4.1.2 "><p id="p15941366336"><a name="p15941366336"></a><a name="p15941366336"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="50.690000000000005%" headers="mcps1.1.4.1.3 "><p id="p11227331716"><a name="p11227331716"></a><a name="p11227331716"></a>Event type. The value <strong id="b46279714239"><a name="b46279714239"></a><a name="b46279714239"></a>change</strong> indicates a data change event.</p>
</td>
</tr>
<tr id="row41225333715"><td class="cellrowborder" valign="top" width="17.119999999999997%" headers="mcps1.1.4.1.1 "><p id="p11122183312719"><a name="p11122183312719"></a><a name="p11122183312719"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="32.190000000000005%" headers="mcps1.1.4.1.2 "><p id="p1012218331376"><a name="p1012218331376"></a><a name="p1012218331376"></a>Callback&lt;<a href="#section256244135613">StorageObserver</a>&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="50.690000000000005%" headers="mcps1.1.4.1.3 "><p id="p181227334710"><a name="p181227334710"></a><a name="p181227334710"></a>Callback object.</p>
</td>
</tr>
</tbody>
</table>
- Example:
```
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
storage.on('change', observer)
storage.putSync('startup', 'auto')
storage.flushSync() // observer will be called.
```
### off \('change'\)<a name="section6421153815"></a>
off\(type: 'change', callback: Callback<StorageObserver\>\): void
Unsubscribes from data changes. Unsubscribes from data changes.
- Parameters **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<a name="table84837251480"></a> - Parameters
<table><thead align="left"><tr id="row1648312251283"><th class="cellrowborder" valign="top" width="17.05%" id="mcps1.1.4.1.1"><p id="p1448317251186"><a name="p1448317251186"></a><a name="p1448317251186"></a>Name</p> | Name| Type| Description|
</th> | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="32.26%" id="mcps1.1.4.1.2"><p id="p1748352512812"><a name="p1748352512812"></a><a name="p1748352512812"></a>Type</p> | type | string | Event type. The value **change** indicates data change events.|
</th> | callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|
<th class="cellrowborder" valign="top" width="50.690000000000005%" id="mcps1.1.4.1.3"><p id="p1448382520818"><a name="p1448382520818"></a><a name="p1448382520818"></a>Description</p>
</th> - Example
</tr> ```
</thead> var observer = function (key) {
<tbody><tr id="row94831251086"><td class="cellrowborder" valign="top" width="17.05%" headers="mcps1.1.4.1.1 "><p id="p348332516813"><a name="p348332516813"></a><a name="p348332516813"></a>type</p> console.info("The key of " + key + " changed.")
</td> }
<td class="cellrowborder" valign="top" width="32.26%" headers="mcps1.1.4.1.2 "><p id="p88671011183314"><a name="p88671011183314"></a><a name="p88671011183314"></a>string</p> storage.off('change', observer)
</td> ```
<td class="cellrowborder" valign="top" width="50.690000000000005%" headers="mcps1.1.4.1.3 "><p id="p1486721113334"><a name="p1486721113334"></a><a name="p1486721113334"></a>Event type. The value <strong id="b10312165319239"><a name="b10312165319239"></a><a name="b10312165319239"></a>change</strong> indicates a data change event.</p>
</td>
</tr> ## StorageObserver
<tr id="row148410251888"><td class="cellrowborder" valign="top" width="17.05%" headers="mcps1.1.4.1.1 "><p id="p9484142519818"><a name="p9484142519818"></a><a name="p9484142519818"></a>callback</p>
</td> **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
<td class="cellrowborder" valign="top" width="32.26%" headers="mcps1.1.4.1.2 "><p id="p1448412251388"><a name="p1448412251388"></a><a name="p1448412251388"></a>Callback&lt;<a href="#section256244135613">StorageObserver</a>&gt;</p>
</td> | Name| Type| Mandatory| Description|
<td class="cellrowborder" valign="top" width="50.690000000000005%" headers="mcps1.1.4.1.3 "><p id="p34843252811"><a name="p34843252811"></a><a name="p34843252811"></a>Callback object to be canceled.</p> | -------- | -------- | -------- | -------- |
</td> | key | string | No| Data changed.|
</tr>
</tbody>
</table>
- Example:
```
var observer = function (key) {
console.info("The key of " + key + " changed.")
}
storage.off('change', observer)
```
## StorageObserver<a name="section256244135613"></a>
<a name="table0103172561814"></a>
<table><thead align="left"><tr id="row12103152513185"><th class="cellrowborder" valign="top" width="17.911791179117913%" id="mcps1.1.5.1.1"><p id="p1610322561817"><a name="p1610322561817"></a><a name="p1610322561817"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="12.291229122912291%" id="mcps1.1.5.1.2"><p id="p20103132551816"><a name="p20103132551816"></a><a name="p20103132551816"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="10.551055105510551%" id="mcps1.1.5.1.3"><p id="p1103172518188"><a name="p1103172518188"></a><a name="p1103172518188"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="59.245924592459254%" id="mcps1.1.5.1.4"><p id="p19103125141812"><a name="p19103125141812"></a><a name="p19103125141812"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row201031125101812"><td class="cellrowborder" valign="top" width="17.911791179117913%" headers="mcps1.1.5.1.1 "><p id="p5549181319415"><a name="p5549181319415"></a><a name="p5549181319415"></a>key</p>
</td>
<td class="cellrowborder" valign="top" width="12.291229122912291%" headers="mcps1.1.5.1.2 "><p id="p39351925253"><a name="p39351925253"></a><a name="p39351925253"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="10.551055105510551%" headers="mcps1.1.5.1.3 "><p id="p8935162182512"><a name="p8935162182512"></a><a name="p8935162182512"></a>No</p>
</td>
<td class="cellrowborder" valign="top" width="59.245924592459254%" headers="mcps1.1.5.1.4 "><p id="p1179214508910"><a name="p1179214508910"></a><a name="p1179214508910"></a>Data changed</p>
</td>
</tr>
</tbody>
</table>
...@@ -1268,6 +1268,8 @@ ...@@ -1268,6 +1268,8 @@
——>——>——>——> Data Management ——>——>——>——> Data Management
——>——>——>——>——> [Lightweight Storage](application-dev/reference/apis/js-apis-data-preferences.md)
——>——>——>——>——> [Lightweight Storage (deprecated since 8)](application-dev/reference/apis/js-apis-data-storage.md) ——>——>——>——>——> [Lightweight Storage (deprecated since 8)](application-dev/reference/apis/js-apis-data-storage.md)
——>——>——>——>——> [Distributed Data Management](application-dev/reference/apis/js-apis-distributed-data.md) ——>——>——>——>——> [Distributed Data Management](application-dev/reference/apis/js-apis-distributed-data.md)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册