Preferences provide capabilities for processing data in the form of key-value (KV) pairs and supports 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, keys are of the string type, and values can be of the number, string, or Boolean type.
> **NOTE**<br/>
...
...
@@ -19,8 +19,8 @@ import data_preferences from '@ohos.data.preferences';
| key | string | Yes| Key of the data to check. It cannot be empty.|
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the **Preferences** instance contains data with the specified key; returns **false** otherwise.|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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.|
**Example**
```ts
...
...
@@ -364,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.|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to check. It cannot be empty.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<boolean> | Promise used to return the result.|
| 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.|
**Example**
```ts
...
...
@@ -383,7 +429,7 @@ promise.then((isExist) => {
console.info("The key of startup is not contained.")
}
}).catch((err)=>{
console.info("Check the key of startup failed, err: "+err)
console.info("Failed to check the key of startup, err: "+err)
})
```
...
...
@@ -397,10 +443,10 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This
| type | string | Event type. The value **change** indicates data change events.|
| callback | Callback<{ key : string }> | Callback used to return data changes.|
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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.|
@@ -68,7 +68,7 @@ Sets the value in the cache based on the specified key.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data to set.|
| value | string | Yes| New value to set. The maximum length is 128 bytes.|
| value | string | Yes| New value to set. The length must be less than 128 bytes.|
| success | Function | No| Called when **storage.set()** is successful.|
| fail | Function | No| Called when **storage.set()** fails. In the callback, **data** indicates the error information, and **code** indicates the error code.|
| complete | Function | No| Called when **storage.set()** is complete.|