@@ -30,9 +30,9 @@ The preference persistent file of an application is stored in the application sa
...
@@ -30,9 +30,9 @@ The preference persistent file of an application is stored in the application sa
The following table lists the APIs used for persisting user preference data. For more information about the APIs, see [User Preferences](../reference/apis/js-apis-data-preferences.md).
The following table lists the APIs used for persisting user preference data. For more information about the APIs, see [User Preferences](../reference/apis/js-apis-data-preferences.md).
| putSync(key: string, value: ValueType): void | Writes data to the Preferences instance. You can use **flush()** to persist the **Preferences** instance data. An asynchronous API is also provided. |
| putSync(key: string, value: ValueType): void | Writes data to the Preferences instance. You can use **flush()** to persist the **Preferences** instance data. An asynchronous API is also provided. |
| hasSync(key: string): void | Checks whether the **Preferences** instance contains a KV pair with the given key. The key cannot be empty. An asynchronous API is also provided. |
| hasSync(key: string): void | Checks whether the **Preferences** instance contains a KV pair with the given key. The key cannot be empty. An asynchronous API is also provided. |
| getSync(key: string, defValue: ValueType): void | Obtains the value of the specified key. If the value is null or not of the default value type, **defValue** is returned. An asynchronous API is also provided. |
| getSync(key: string, defValue: ValueType): void | Obtains the value of the specified key. If the value is null or not of the default value type, **defValue** is returned. An asynchronous API is also provided. |
...
@@ -40,7 +40,7 @@ The following table lists the APIs used for persisting user preference data. For
...
@@ -40,7 +40,7 @@ The following table lists the APIs used for persisting user preference data. For
| flush(callback: AsyncCallback<void>): void | Flushes the data of this **Preferences** instance to a file for data persistence. |
| flush(callback: AsyncCallback<void>): void | Flushes the data of this **Preferences** instance to a file for data persistence. |
| on(type: 'change', callback: Callback<{ key : string }>): void | Subscribes to data changes of the specified key. When the value of the specified key is changed and saved by **flush()**, a callback will be invoked to return the new data. |
| on(type: 'change', callback: Callback<{ key : string }>): void | Subscribes to data changes of the specified key. When the value of the specified key is changed and saved by **flush()**, a callback will be invoked to return the new data. |
| off(type: 'change', callback?: Callback<{ key : string }>): void | Unsubscribes from data changes. |
| off(type: 'change', callback?: Callback<{ key : string }>): void | Unsubscribes from data changes. |
| deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void | Deletes a **Preferences** instance from memory. If the **Preferences** instance has a persistent file, this API also deletes the persistent file.|
| deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void | Deletes a **Preferences** instance from memory. If the **Preferences** instance has a persistent file, this API also deletes the persistent file. |
## How to Develop
## How to Develop
...
@@ -58,11 +58,13 @@ The following table lists the APIs used for persisting user preference data. For
...
@@ -58,11 +58,13 @@ The following table lists the APIs used for persisting user preference data. For
console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`);
return;
return;
...
@@ -102,7 +105,7 @@ The following table lists the APIs used for persisting user preference data. For
...
@@ -102,7 +105,7 @@ The following table lists the APIs used for persisting user preference data. For
3. Write data.
3. Write data.
Use **putSync()** to write data to the cached **Preferences** instance. After data is written, you can use **flush()** to persist the **Preferences** instance data to a file if necessary.
Use **putSync()** to save data to the cached **Preferences** instance. After data is written, you can use **flush()** to persist the **Preferences** instance data to a file if necessary.
> **NOTE**
> **NOTE**
>
>
...
@@ -157,7 +160,7 @@ The following table lists the APIs used for persisting user preference data. For
...
@@ -157,7 +160,7 @@ The following table lists the APIs used for persisting user preference data. For
```js
```js
try{
try{
preferences.flush((err)=>{
preferences.flush((err:BusinessError)=>{
if(err){
if(err){
console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);
console.error(`Failed to flush. Code:${err.code}, message:${err.message}`);
return;
return;
...
@@ -174,18 +177,20 @@ The following table lists the APIs used for persisting user preference data. For
...
@@ -174,18 +177,20 @@ The following table lists the APIs used for persisting user preference data. For
Specify an observer as the callback to return the 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. Example:
Specify an observer as the callback to return the 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. Example: