console.info("Failed to get the value of 'startup'. Cause: "+err);
console.info("Failed to get the value of 'startup'. Cause: "+err);
})
})
}).catch((err)=>{
console.info("Failed to get the preferences.")
});
```
```
5. Store data persistently.
5. Store data persistently.
...
@@ -161,11 +167,12 @@ Use the following APIs to delete a **Preferences** instance or data 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.
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
```js
varobserver=function(key){
letobserver=function(key){
console.info("The key"+key+" changed.");
console.info("The key"+key+" changed.");
}
}
preferences.on('change',observer);
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){
if(err){
console.info("Failed to put the value of 'startup'. Cause: "+err);
console.info("Failed to put the value of 'startup'. Cause: "+err);
| 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.|
| 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.|
| 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.|
| 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. |
| 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.|
| 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.|
| 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. |
| 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.|
| 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.|
| 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.|
| 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
...
@@ -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. |
| 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
...
@@ -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. |
| 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**
**Example**
```js
```js
preferences.put('startup','auto',function(err){
preferences.put('startup','auto',function(err){
if(err){
if(err){
...
@@ -370,17 +547,20 @@ Writes data to this **Preferences** instance. This API uses a promise to return
...
@@ -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. |
| 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.|
| 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. |
| 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.|
| 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**
**Example**
```js
```js
preferences.has('startup',function(err,isExist){
preferences.has('startup',function(err,isExist){
if(err){
if(err){
...
@@ -425,16 +607,18 @@ preferences.has('startup', function (err, isExist) {
...
@@ -425,16 +607,18 @@ preferences.has('startup', function (err, isExist) {
has(key: string): Promise<boolean>
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.|
| 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
...
@@ -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. |
| 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.|
| 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**
**Example**
```js
```js
preferences.delete('startup',function(err){
preferences.delete('startup',function(err){
if(err){
if(err){
...
@@ -490,16 +676,19 @@ Deletes a KV pair from this **Preferences** instance. This API uses a promise to
...
@@ -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.|
| 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**
**Example**
```js
```js
preferences.clear(function(err){
preferences.clear(function(err){
if(err){
if(err){
...
@@ -593,11 +788,13 @@ Clears this **Preferences** instance. This API uses a promise to return the resu
...
@@ -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. |
| 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.|
| 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.|