console.info("Failed to get the value of 'startup'. Cause: "+err);
})
}).catch((err)=>{
console.info("Failed to get the preferences.")
});
console.info("Failed to get the value of 'startup'. Cause: "+err);
})
```
5. Store data persistently.
Use the **flush()** method to flush data from the **Preferences** instance to its file.
...
...
@@ -161,11 +167,12 @@ Use the following APIs to delete a **Preferences** instance or data file.
Specify an observer as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and saved by **flush()**, the observer callback will be invoked to return the new data.
```js
varobserver=function(key){
letobserver=function(key){
console.info("The key"+key+" changed.");
}
preferences.on('change',observer);
preferences.put('startup','auto',function(err){
// The data is changed from 'auto' to 'manual'.
preferences.put('startup','manual',function(err){
if(err){
console.info("Failed to put the value of 'startup'. Cause: "+err);
| name | string | Yes | Name of the **Preferences** instance.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance.|
| callback | AsyncCallback<[Preferences](#preferences)> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **object** is the **Preferences** instance obtained. Otherwise, **err** is an error code.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance to delete. |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance to delete.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance to remove. |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.|
| context | Context | Yes | Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md). |
| name | string | Yes | Name of the **Preferences** instance to remove.|
| callback | AsyncCallback<Object> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **value** is the **Object** instance obtained. Otherwise, **err** is an error code.|
...
...
@@ -318,11 +491,13 @@ Obtains an **Object** instance that contains all KV pairs. This API uses a promi
| key | string | Yes | Key of the data. It cannot be empty. |
...
...
@@ -350,6 +526,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is undefined. Otherwise, **err** is an error code. |
**Example**
```js
preferences.put('startup','auto',function(err){
if(err){
...
...
@@ -370,17 +547,20 @@ Writes data to this **Preferences** instance. This API uses a promise to return
| key | string | Yes | Key of the data. It cannot be empty. |
| value | [ValueType](#valuetype) | Yes | Value to write. The value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values.|
| key | string | Yes | Key of the data to check. It cannot be empty. |
| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.|
**Example**
```js
preferences.has('startup',function(err,isExist){
if(err){
...
...
@@ -425,16 +607,18 @@ preferences.has('startup', function (err, isExist) {
has(key: string): Promise<boolean>
Checks whether this **Preferences** instance contains data with the given key. This API uses a promise to return the result.
Checks whether this **Preferences** instance contains a KV pair with the given key. This API uses a promise to return the result..
| Promise<boolean> | Promise used to return the result. If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.|
...
...
@@ -464,12 +648,14 @@ Deletes a KV pair from this **Preferences** instance. This API uses an asynchron
| key | string | Yes | Key of the KV pair to delete. It cannot be empty. |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.|
**Example**
```js
preferences.delete('startup',function(err){
if(err){
...
...
@@ -490,16 +676,19 @@ Deletes a KV pair from this **Preferences** instance. This API uses a promise to
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.|
**Example**
```js
preferences.clear(function(err){
if(err){
...
...
@@ -593,11 +788,13 @@ Clears this **Preferences** instance. This API uses a promise to return the resu
| type | string | Yes | Event type to unsubscribe from. The value **change** indicates data change events. |
| callback | Callback<{ key : string }> | No | Callback to unregister. If this parameter is left blank, the callbacks used to subscribing to all data changes will be unregistered.|