未验证 提交 11eb6577 编写于 作者: O openharmony_ci 提交者: Gitee

!6732 【翻译完成】#I5CJD2

Merge pull request !6732 from Annie_wang/PR5430
# Preferences Development # Preferences Development
> **NOTE**
>
> This feature is supported since API Version 9. For the versions earlier than API Version 9, use [Lightweight Storage](../reference/apis/js-apis-data-storage.md) APIs.
## When to Use ## When to Use
Preferences are ideal for storing data frequently used by applications, but not for storing a large amount of data or data with frequent changes. The application data is persistently stored on a device in the form of files. Note that the instance accessed by an application contains all data of the file. The data is always loaded to the memory of the device until the application removes it from the memory. The application can perform data operations using the **Preferences** APIs. Preferences are ideal for storing data frequently used by applications, but not for storing a large amount of data or data with frequent changes. The application data is persistently stored on a device in the form of files. Note that the instance accessed by an application contains all data of the file. The data is always loaded to the memory of the device until the application removes it from the memory. The application can perform data operations using the **Preferences** APIs.
## Available APIs ## Available APIs
Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type. Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type. For more APIs related to preferences, see [Preferences](../reference/apis/js-apis-data-preferences.md).
### Creating a Preferences Instance ### Creating a Preferences Instance
...@@ -26,7 +30,7 @@ Call the **put()** method to add or modify data in a **Preferences** instance. ...@@ -26,7 +30,7 @@ Call the **put()** method to add or modify data in a **Preferences** instance.
| Class | API | Description | | Class | API | Description |
| ------- | -------------------------------------------------- | ----------------------------------------------- | | ------- | -------------------------------------------------- | ----------------------------------------------- |
| Preferences | put(key: string, value: ValueType): Promise\<void> | Writes data of the number, string, and Boolean types.| | Preferences | put(key: string, value: ValueType): Promise\<void> | Writes data with the value type of number, string, boolean, Array\<number>, Array\<string>, or Array\<boolean>.|
### Reading Data ### Reading Data
...@@ -36,7 +40,7 @@ Call the **get()** method to read data from a **Preferences** instance. ...@@ -36,7 +40,7 @@ Call the **get()** method to read data from a **Preferences** instance.
| Class | API | Description | | Class | API | Description |
| ------- | ---------------------------------------------------------- | ----------------------------------------------- | | ------- | ---------------------------------------------------------- | ----------------------------------------------- |
| Preferences | get(key: string, defValue: ValueType): Promise\<ValueType> | Reads data of the number, string, and Boolean types.| | Preferences | get(key: string, defValue: ValueType): Promise\<ValueType> | Obtains data with the value type of number, string, boolean, Array\<number>, Array\<string>, or Array\<boolean>.|
### Storing Data Persistently ### Storing Data Persistently
...@@ -67,7 +71,7 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -67,7 +71,7 @@ Use the following APIs to delete a **Preferences** instance or data file.
| Package | API | Description | | Package | API | Description |
| ----------------- | ---------------------------------------------------- | ------------------------------------------------------------ | | ----------------- | ---------------------------------------------------- | ------------------------------------------------------------ |
| ohos.data.preferences | deletePreferences(context: Context, name: string): Promise<void>; | Deletes a **Preferences** instance from the cache and deletes its file from the device.| | ohos.data.preferences | deletePreferences(context: Context, name: string): Promise\<void>; | Deletes a **Preferences** instance from the cache and deletes its file from the device.|
| ohos.data.preferences | removePreferencesFromCache(context: Context, name: string): Promise\<void>; | Removes a **Preferences** instance from the memory to release memory. | ohos.data.preferences | removePreferencesFromCache(context: Context, name: string): Promise\<void>; | Removes a **Preferences** instance from the memory to release memory.
## How to Develop ## How to Develop
...@@ -91,8 +95,8 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -91,8 +95,8 @@ Use the following APIs to delete a **Preferences** instance or data file.
```js ```js
promise.then((preferences) => { promise.then((preferences) => {
let getPromise = preferences.put('startup', 'auto') let putPromise = preferences.put('startup', 'auto')
getPromise.then(() => { putPromise.then(() => {
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Failed to put the value of startup with err: " + err) console.info("Failed to put the value of startup with err: " + err)
......
...@@ -13,7 +13,7 @@ Lightweight storage provides applications with data processing capability and al ...@@ -13,7 +13,7 @@ Lightweight storage provides applications with data processing capability and al
## Modules to Import ## Modules to Import
```js ```js
import dataStorage from '@ohos.data.storage'; import data_storage from '@ohos.data.storage';
``` ```
## Constants ## Constants
...@@ -26,7 +26,7 @@ import dataStorage from '@ohos.data.storage'; ...@@ -26,7 +26,7 @@ import dataStorage from '@ohos.data.storage';
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.| | MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.|
## dataStorage.getStorageSync ## data_storage.getStorageSync
getStorageSync(path: string): Storage getStorageSync(path: string): Storage
...@@ -46,24 +46,17 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -46,24 +46,17 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let path = '/data/storage/el2/database'
context.getFilesDir((err, path) => { let storage = data_storage.getStorageSync(path + '/mystore')
if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err));
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let storage = dataStorage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto')
storage.flushSync() storage.flushSync()
});
``` ```
## dataStorage.getStorage ## data_storage.getStorage
getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void
...@@ -79,29 +72,21 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -79,29 +72,21 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let path = '/data/storage/el2/database'
context.getFilesDir((err, path) => { data_storage.getStorage(path + '/mystore', function (err, storage) {
if (err) { if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err)); console.info("Failed to get the storage. Path: " + path + '/mystore')
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
dataStorage.getStorage(path + '/mystore', function (err, storage) {
if (err) {
console.info("Get the storage failed, path: " + path + '/mystore')
return; return;
} }
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto')
storage.flushSync() storage.flushSync()
}) })
});
``` ```
## dataStorage.getStorage ## data_storage.getStorage
getStorage(path: string): Promise&lt;Storage&gt; getStorage(path: string): Promise&lt;Storage&gt;
...@@ -121,28 +106,21 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -121,28 +106,21 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let path = '/data/storage/el2/database'
context.getFilesDir((err, path) => {
if (err) { let getPromise = data_storage.getStorage(path + '/mystore')
console.info("Get the storage failed, path: " + path + '/mystore') getPromise.then((storage) => {
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let promisegetSt = dataStorage.getStorage(path + '/mystore')
promisegetSt.then((storage) => {
storage.putSync('startup', 'auto') storage.putSync('startup', 'auto')
storage.flushSync() storage.flushSync()
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed, path: " + path + '/mystore') console.info("Failed to get the storage. Path: " + path + '/mystore')
}) })
});
``` ```
## dataStorage.deleteStorageSync ## data_storage.deleteStorageSync
deleteStorageSync(path: string): void deleteStorageSync(path: string): void
...@@ -157,11 +135,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -157,11 +135,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**Example** **Example**
```js ```js
dataStorage.deleteStorageSync(path + '/mystore') let path = '/data/storage/el2/database'
data_storage.deleteStorageSync(path + '/mystore')
``` ```
## dataStorage.deleteStorage ## data_storage.deleteStorage
deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -173,11 +152,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -173,11 +152,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.| | path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
dataStorage.deleteStorage(path + '/mystore', function (err) { let path = '/data/storage/el2/database'
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Deleted failed with err: " + err) console.info("Deleted failed with err: " + err)
return return
...@@ -187,7 +167,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -187,7 +167,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
``` ```
## dataStorage.deleteStorage ## data_storage.deleteStorage
deleteStorage(path: string): Promise&lt;void&gt; deleteStorage(path: string): Promise&lt;void&gt;
...@@ -203,11 +183,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -203,11 +183,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promisedelSt = dataStorage.deleteStorage(path + '/mystore') let path = '/data/storage/el2/database'
let promisedelSt = data_storage.deleteStorage(path + '/mystore')
promisedelSt.then(() => { promisedelSt.then(() => {
console.info("Deleted successfully.") console.info("Deleted successfully.")
}).catch((err) => { }).catch((err) => {
...@@ -216,7 +197,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -216,7 +197,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
``` ```
## dataStorage.removeStorageFromCacheSync ## data_storage.removeStorageFromCacheSync
removeStorageFromCacheSync(path: string): void removeStorageFromCacheSync(path: string): void
...@@ -231,11 +212,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -231,11 +212,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**Example** **Example**
```js ```js
dataStorage.removeStorageFromCacheSync(path + '/mystore') let path = '/data/storage/el2/database'
data_storage.removeStorageFromCacheSync(path + '/mystore')
``` ```
## dataStorage.removeStorageFromCache ## data_storage.removeStorageFromCache
removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -247,11 +229,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -247,11 +229,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.| | path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
dataStorage.removeStorageFromCache(path + '/mystore', function (err) { let path = '/data/storage/el2/database'
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Removed storage from cache failed with err: " + err) console.info("Removed storage from cache failed with err: " + err)
return return
...@@ -261,7 +244,7 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -261,7 +244,7 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
``` ```
## dataStorage.removeStorageFromCache ## data_storage.removeStorageFromCache
removeStorageFromCache(path: string): Promise&lt;void&gt; removeStorageFromCache(path: string): Promise&lt;void&gt;
...@@ -277,11 +260,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -277,11 +260,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promiserevSt = dataStorage.removeStorageFromCache(path + '/mystore') let path = '/data/storage/el2/database'
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => { promiserevSt.then(() => {
console.info("Removed storage from cache successfully.") console.info("Removed storage from cache successfully.")
}).catch((err) => { }).catch((err) => {
...@@ -412,7 +396,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -412,7 +396,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.| | key | string | Yes| Key of the data. It cannot be empty.|
| value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.| | value | [ValueType](#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.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -443,7 +427,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -443,7 +427,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -578,7 +562,7 @@ Deletes data with the specified key from this storage object. This API uses an a ...@@ -578,7 +562,7 @@ Deletes data with the specified key from this storage object. This API uses an a
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.| | key | string | Yes| Key of the data. It cannot be empty.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -608,7 +592,7 @@ Deletes data with the specified key from this storage object. This API uses a pr ...@@ -608,7 +592,7 @@ Deletes data with the specified key from this storage object. This API uses a pr
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -646,7 +630,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -646,7 +630,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -671,7 +655,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -671,7 +655,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -709,7 +693,7 @@ Clears this **Storage** object. This API uses an asynchronous callback to return ...@@ -709,7 +693,7 @@ Clears this **Storage** object. This API uses an asynchronous callback to return
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -734,7 +718,7 @@ Clears this **Storage** object. This API uses a promise to return the result. ...@@ -734,7 +718,7 @@ Clears this **Storage** object. This API uses a promise to return the result.
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册