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.
Preferences provide capabilities for processing key-value (KV) data for applications and supports lightweight data persistence, modification, and query. Data is stored in KV pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
> 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.
> 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
## Modules to Import
```
```ts
import data_preferences from '@ohos.data.preferences'
Reads a file and loads the data to the **Preferences** instance. This method uses an asynchronous callback to return the execution result.
Reads a preference persistence file and loads data to the **Preferences** instance for data operations. This API uses an asynchronous callback to return the result.
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.
Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
Once a preference persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the execution result.
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.
Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
Once a preference persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the execution result.
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.
Removes a **Preferences** singleton instance from the memory.
This method uses an asynchronous callback to return the result.
When a **Preferences** singleton instance is removed, this instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the execution result.
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.
Removes a **Preferences** singleton instance from the memory.
This method uses a promise to return the result.
When a **Preferences** singleton instance is removed, this instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the execution result.
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 of a key. If the value is null or a non-default value, the default data is returned. This API uses an asynchronous callback to return the result.
This method uses an asynchronous callback to return the result.
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()**.
Obtain a **Preferences** instance, writes data to the **Preferences** instance, and saves the data to the file using **flush()** or **flushSync()**. This API uses an asynchronous callback to return the execution result.
This method uses an asynchronous callback to return the result.
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()**.
Obtain a **Preferences** instance, writes data to the **Preferences** instance, and saves the data to the file using **flush()** or **flushSync()**. This API uses a promise to return the execution result.
console.info("Delete startup key failed with err: " + err)
console.info("Failed to delete startup key, err: "+err)
})
}).catch((err)=>{
console.info("Failed to get the preferences, err: "+err)
})
})
}
```
```
...
@@ -497,9 +587,7 @@ This method uses a promise to return the result.
...
@@ -497,9 +587,7 @@ This method uses a promise to return the result.
flush(callback: AsyncCallback<void>): void
flush(callback: AsyncCallback<void>): void
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file.
Saves the modification to this **Preferences** instance and synchronizes the modification to the preference persistence file. This API uses an asynchronous callback to return the result.
This method uses an asynchronous callback to return the result.
console.info("Failed to get the preferences, err: "+err)
return
}
preferences.flush(function(err){
preferences.flush(function(err){
if(err){
if(err){
console.info("Flush to file failed with err: " + err)
console.info("Failed to flush data to file, err: "+err)
return
return
}
}
console.info("Flushed to file successfully.")
console.info("Flushed data to file successfully.")
})
})
})
}
```
```
...
@@ -524,25 +623,33 @@ This method uses an asynchronous callback to return the result.
...
@@ -524,25 +623,33 @@ This method uses an asynchronous callback to return the result.
flush(): Promise<void>
flush(): Promise<void>
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file.
Saves the modification to this **Preferences** instance and synchronizes the modification to the preference persistence file. This API uses a promise to return the result.