@@ -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.
```js
promise.then((storage) => {
let getPromise = storage.get('startup', 'default');
getPromise.then((value) => {
console.info("The value of startup is " + value);
}).catch((err) => {
console.info("Failed to get the value of startup with err: " + err);
})
}).catch((err) => {
console.info("Failed to get the storage.");
})
```
```js
promise.then((storage)=>{
letgetPromise=storage.get('startup','default')
getPromise.then((value)=>{
console.info("The value of startup is "+value);
}).catch((err)=>{
console.info("Failed to get the value of startup with err: "+err);
})
}).catch((err)=>{
console.info("Failed to get the storage.")
})
```
5. Store data persistently.
...
...
@@ -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.
```js
promise.then((storage) => {
var observer = function (key) {
console.info("The key of " + key + " changed.");
}
storage.on('change', observer);
storage.putSync('startup', 'auto'); // Modify data in the Storage instance.
storage.flushSync(); // Trigger the StorageObserver callback.
```js
promise.then((storage)=>{
varobserver=function(key){
console.info("The key of "+key+" changed.");
}
storage.on('change',observer)
storage.putSync('startup','auto');// Modify data in the Storage instance.
storage.flushSync();// Trigger the StorageObserver callback.
storage.off('change', observer); // Unsubscribe from the data changes.
}).catch((err) => {
console.info("Failed to get the storage.");
})
```
storage.off('change',observer);// Unsubscribe from the data changes.
}).catch((err)=>{
console.info("Failed to get the storage.");
})
```
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.
```js
let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => {
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Failed to deleted the storage with err: " + err);
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
...
...
@@ -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. |
| 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. |
| 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.|