@@ -118,18 +118,19 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -118,18 +118,19 @@ Use the following APIs to delete a **Storage** instance or data file.
Use the **get\(\)** method of the **Storage** class to read data.
Use the **get\(\)** method of the **Storage** class to read data.
```js
```js
promise.then((storage) => {
promise.then((storage)=>{
let getPromise = storage.get('startup', 'default');
letgetPromise=storage.get('startup','default')
getPromise.then((value) => {
getPromise.then((value)=>{
console.info("The value of startup is " + value);
console.info("The value of startup is "+value);
}).catch((err) => {
}).catch((err)=>{
console.info("Failed to get the value of startup with err: " + err);
console.info("Failed to get the value of startup with err: "+err);
})
})
}).catch((err) => {
}).catch((err)=>{
console.info("Failed to get the storage.");
console.info("Failed to get the storage.")
})
```
})
```
5. Store data persistently.
5. Store data persistently.
...
@@ -144,32 +145,32 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -144,32 +145,32 @@ Use the following APIs to delete a **Storage** instance or data file.
Specify **StorageObserver** as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and the **flush\(\)** method is executed, **StorageObserver** will be invoked. Unregister the **StorageObserver** when it is no longer required.
Specify **StorageObserver** as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and the **flush\(\)** method is executed, **StorageObserver** will be invoked. Unregister the **StorageObserver** when it is no longer required.
```js
```js
promise.then((storage) => {
promise.then((storage)=>{
var observer = function (key) {
varobserver=function(key){
console.info("The key of " + key + " changed.");
console.info("The key of "+key+" changed.");
}
}
storage.on('change', observer);
storage.on('change',observer)
storage.putSync('startup', 'auto'); // Modify data in the Storage instance.
storage.putSync('startup','auto');// Modify data in the Storage instance.
storage.flushSync(); // Trigger the StorageObserver callback.
storage.flushSync();// Trigger the StorageObserver callback.
storage.off('change', observer); // Unsubscribe from the data changes.
storage.off('change',observer);// Unsubscribe from the data changes.
}).catch((err) => {
}).catch((err)=>{
console.info("Failed to get the storage.");
console.info("Failed to get the storage.");
})
})
```
```
7. Delete the specified file.
7. Delete the specified file.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
```js
```js
let promise = dataStorage.deleteStorage(path + '/mystore');
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.
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.
> **NOTE**
> **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.
> - 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.
>
> - 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).
>
> - The APIs of this module can be used only in the FA model.
## Modules to Import
## Modules to Import
...
@@ -35,15 +38,15 @@ Reads the specified file and loads its data to the **Storage** instance for data
...
@@ -35,15 +38,15 @@ Reads the specified file and loads its data to the **Storage** instance for data
| key | string | Yes | Key of the data. It cannot be empty. |
| key | string | Yes | Key of the data. It cannot be empty. |
| defValue | [ValueType](#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. |
| defValue | [ValueType](#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.|