> 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.
| 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.
| 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.
| 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)=>{
letgetPromise=preferences.put('startup','auto')
letputPromise=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)
Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support lightweight 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 lightweight data persistence, modification, and query. In KV pairs, the keys are of the string type, and the values can be of the number, string, Boolean, Array\<number>, Array\<string>, or Array\<boolean> type.
> **NOTE**<br/>
> **NOTE**<br/>
...
@@ -33,11 +33,11 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i
...
@@ -33,11 +33,11 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or functionality.|
| context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
| name | string | Yes| Name of the **Preferences** instance persistence file.|
**Return value**
**Return value**
| Type| Description|
| Type| Description|
| -------- | -------- |
| -------- | -------- |
| Promise<void> | Promise used to return the result.|
| Promise<void> | Promise that returns no value.|
**Example**
**Example**
```ts
```ts
...
@@ -218,7 +218,7 @@ Obtains the value of a key. If the value is null or a non-default value, the def
...
@@ -218,7 +218,7 @@ Obtains the value of a key. If the value is null or a non-default value, the def
| Name| Type| Mandatory| Description|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to obtain. It cannot be empty.|
| key | string | Yes| Key of the data to obtain. It cannot be empty.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.
| callback | AsyncCallback<ValueType> | Yes| Callback used to return the result.|
| callback | AsyncCallback<ValueType> | Yes| Callback used to return the result.|
**Example**
**Example**
...
@@ -241,16 +241,16 @@ Obtains the value of a key. If the value is null or a non-default value, the def
...
@@ -241,16 +241,16 @@ Obtains the value of a key. If the value is null or a non-default value, the def
| key | string | Yes| Key of the data to obtain. It cannot be empty.|
| key | string | Yes| Key of the data to obtain. It cannot be empty.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.
**Return value**
**Return value**
| Type| Description|
| Type| Description|
| -------- | -------- |
| -------- | -------- |
| Promise<ValueType> | Promise used to return the result.|
| Promise<ValueType> | Promise used to return the result.|
**Example**
**Example**
```ts
```ts
...
@@ -271,13 +271,13 @@ Obtains the **Object** instance that contains all KV pairs.
...
@@ -271,13 +271,13 @@ Obtains the **Object** instance that contains all KV pairs.
| 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, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.|
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
| callback | AsyncCallback<void> | Yes| Callback that returns no value.|
**Example**
**Example**
```ts
```ts
...
@@ -350,15 +350,15 @@ Puts a new value to this **Preferences** instance and its persistence file. This
...
@@ -350,15 +350,15 @@ Puts a new value to this **Preferences** instance and its persistence file. This
| 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, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.|
**Return value**
**Return value**
| Type| Description|
| Type| Description|
| -------- | -------- |
| -------- | -------- |
| Promise<void> | Promise used to return the result.|
| Promise<void> | Promise that returns no value.|
**Example**
**Example**
```ts
```ts
...
@@ -380,10 +380,10 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
...
@@ -380,10 +380,10 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
| key | string | Yes| Key of the data to check. It cannot be empty.|
| key | string | Yes| Key of the data to check. It cannot be empty.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
**Example**
**Example**
```ts
```ts
...
@@ -410,14 +410,14 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
...
@@ -410,14 +410,14 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
| key | string | Yes| Key of the data to check. It cannot be empty.|
| key | string | Yes| Key of the data to check. It cannot be empty.|
**Return value**
**Return value**
| Type| Description|
| Type| Description|
| -------- | -------- |
| -------- | -------- |
| Promise<boolean> | Promise used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
| Promise<boolean> | Promise used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
**Example**
**Example**
```ts
```ts
...
@@ -443,10 +443,10 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This
...
@@ -443,10 +443,10 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This
| type | string | Yes| Event type. The value **change** indicates data change events.|
| type | string | Yes| Event type. The value **change** indicates data change events.|
| callback | Callback<{ key : string }> | No| Callback used to return data changes. If this parameter is left empty, all callbacks for data changes will be canceled.|
| callback | Callback<{ key : string }> | No| Callback used to return data changes. If this parameter is left empty, all callbacks for data changes will be canceled.|