未验证 提交 d44ed7c0 编写于 作者: O openharmony_ci 提交者: Gitee

!9993 [翻译完成】#I5P4W5

Merge pull request !9993 from Annie_wang/PR8973
...@@ -39,8 +39,8 @@ Obtains a **Preferences** instance. This API uses an asynchronous callback to re ...@@ -39,8 +39,8 @@ Obtains a **Preferences** instance. This API uses an asynchronous callback to re
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | | -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | context | [Context](js-apis-ability-context.md) | Yes | Application context. |
| 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 the **Preferences** instance obtained is returned. Otherwise, **err** is an error code.|
**Example** **Example**
...@@ -66,12 +66,14 @@ Obtains a **Preferences** instance. This API uses a promise to return the result ...@@ -66,12 +66,14 @@ Obtains a **Preferences** instance. This API uses a promise to return the result
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description |
| ------- | ------------------------------------- | ---- | -------------------------- | | Name | Type | Mandatory| Description |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. |
| name | string | Yes | Name of the **Preferences** instance.| | name | string | Yes | Name of the **Preferences** instance.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------------------------------ | ---------------------------------- | | ------------------------------------------ | ---------------------------------- |
| Promise<[Preferences](#preferences)> | Promise used to return the **Preferences** instance obtained.| | Promise<[Preferences](#preferences)> | Promise used to return the **Preferences** instance obtained.|
...@@ -103,6 +105,7 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi ...@@ -103,6 +105,7 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | context | [Context](js-apis-ability-context.md) | Yes | Application context. |
...@@ -110,6 +113,7 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi ...@@ -110,6 +113,7 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi
| 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
data_preferences.deletePreferences(this.context, 'mystore', function (err) { data_preferences.deletePreferences(this.context, 'mystore', function (err) {
if (err) { if (err) {
...@@ -134,17 +138,20 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi ...@@ -134,17 +138,20 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description |
| ------- | ------------------------------------- | ---- | -------------------------- | | Name | Type | Mandatory| Description |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. |
| name | string | Yes | Name of the **Preferences** instance to delete.| | name | string | Yes | Name of the **Preferences** instance to delete.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise<void> | Promise that returns no value.| | Promise<void> | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = data_preferences.deletePreferences(this.context, 'mystore') let promise = data_preferences.deletePreferences(this.context, 'mystore')
promise.then(() => { promise.then(() => {
...@@ -166,6 +173,7 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi ...@@ -166,6 +173,7 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | context | [Context](js-apis-ability-context.md) | Yes | Application context. |
...@@ -173,6 +181,7 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi ...@@ -173,6 +181,7 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi
| 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
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
if (err) { if (err) {
...@@ -195,17 +204,20 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi ...@@ -195,17 +204,20 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description |
| ------- | ------------------------------------- | ---- | -------------------------- | | Name | Type | Mandatory| Description |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. | | ------- | ------------------------------------- | ---- | ----------------------- |
| context | [Context](js-apis-ability-context.md) | Yes | Application context. |
| name | string | Yes | Name of the **Preferences** instance to remove.| | name | string | Yes | Name of the **Preferences** instance to remove.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise<void> | Promise that returns no value.| | Promise<void> | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore') let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
promise.then(() => { promise.then(() => {
...@@ -232,6 +244,7 @@ Obtains the value of a key. This API uses an asynchronous callback to return the ...@@ -232,6 +244,7 @@ Obtains the value of a key. This API uses an asynchronous callback to return the
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| 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. |
...@@ -260,6 +273,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the ...@@ -260,6 +273,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| 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. |
...@@ -272,6 +286,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the ...@@ -272,6 +286,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the
| Promise<[ValueType](#valuetype)&gt; | Promise used to return the value obtained.| | Promise<[ValueType](#valuetype)&gt; | Promise used to return the value obtained.|
**Example** **Example**
```js ```js
let promise = preferences.get('startup', 'default'); let promise = preferences.get('startup', 'default');
promise.then((data) => { promise.then((data) => {
...@@ -290,6 +305,7 @@ Obtains an **Object** instance that contains all KV pairs. This API uses an asyn ...@@ -290,6 +305,7 @@ Obtains an **Object** instance that contains all KV pairs. This API uses an asyn
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ | | -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;Object&gt; | 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&lt;Object&gt; | 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 +334,13 @@ Obtains an **Object** instance that contains all KV pairs. This API uses a promi ...@@ -318,11 +334,13 @@ Obtains an **Object** instance that contains all KV pairs. This API uses a promi
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value** **Return value**
| Type | Description | | Type | Description |
| --------------------- | ------------------------------------------- | | --------------------- | ------------------------------------------- |
| Promise&lt;Object&gt; | Promise used to return the **Object** instance obtained.| | Promise&lt;Object&gt; | Promise used to return the **Object** instance obtained.|
**Example** **Example**
```js ```js
let promise = preferences.getAll(); let promise = preferences.getAll();
promise.then((value) => { promise.then((value) => {
...@@ -343,6 +361,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call ...@@ -343,6 +361,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ | | -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| key | string | Yes | Key of the data. It cannot be empty. | | key | string | Yes | Key of the data. It cannot be empty. |
...@@ -350,6 +369,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call ...@@ -350,6 +369,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is undefined. Otherwise, **err** is an error code. | | callback | AsyncCallback&lt;void&gt; | 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 +390,20 @@ Writes data to this **Preferences** instance. This API uses a promise to return ...@@ -370,17 +390,20 @@ Writes data to this **Preferences** instance. This API uses a promise to return
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ----------------------- | ---- | ------------------------------------------------------------ | | ------ | ----------------------- | ---- | ------------------------------------------------------------ |
| 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.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = preferences.put('startup', 'auto'); let promise = preferences.put('startup', 'auto');
promise.then(() => { promise.then(() => {
...@@ -400,12 +423,14 @@ Checks whether this **Preferences** instance contains a KV pair of the given key ...@@ -400,12 +423,14 @@ Checks whether this **Preferences** instance contains a KV pair of the given key
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | | -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
| 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&lt;boolean&gt; | 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&lt;boolean&gt; | 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) {
...@@ -430,11 +455,13 @@ Checks whether this **Preferences** instance contains data with the given key. T ...@@ -430,11 +455,13 @@ Checks whether this **Preferences** instance contains data with the given key. T
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------- | | ------ | ------ | ---- | ------------------------------- |
| 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&lt;boolean&gt; | Promise used to return the result. If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.| | Promise&lt;boolean&gt; | 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 +491,14 @@ Deletes a KV pair from this **Preferences** instance. This API uses an asynchron ...@@ -464,12 +491,14 @@ Deletes a KV pair from this **Preferences** instance. This API uses an asynchron
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------------------------------------------------- | | -------- | ------------------------- | ---- | ---------------------------------------------------- |
| 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&lt;void&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.| | callback | AsyncCallback&lt;void&gt; | 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 +519,19 @@ Deletes a KV pair from this **Preferences** instance. This API uses a promise to ...@@ -490,16 +519,19 @@ Deletes a KV pair from this **Preferences** instance. This API uses a promise to
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------- | | ------ | ------ | ---- | ------------------------------- |
| 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.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = preferences.delete('startup'); let promise = preferences.delete('startup');
promise.then(() => { promise.then(() => {
...@@ -514,16 +546,18 @@ promise.then(() => { ...@@ -514,16 +546,18 @@ promise.then(() => {
flush(callback: AsyncCallback&lt;void&gt;): void flush(callback: AsyncCallback&lt;void&gt;): void
Saves the data of the **Preferences** instance to a file asynchronously. This API uses an asynchronous callback to return the result. Asynchronously stores data of this **Preferences** instance to its persistent file. This API uses a asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------------------------------------------------- | | -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.| | callback | AsyncCallback&lt;void&gt; | 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.flush(function (err) { preferences.flush(function (err) {
if (err) { if (err) {
...@@ -539,16 +573,18 @@ preferences.flush(function (err) { ...@@ -539,16 +573,18 @@ preferences.flush(function (err) {
flush(): Promise&lt;void&gt; flush(): Promise&lt;void&gt;
Saves the data of the **Preferences** instance to a file asynchronously. This API uses a promise to return the result. Asynchronously stores data of this **Preferences** instance to its persistent file. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = preferences.flush(); let promise = preferences.flush();
promise.then(() => { promise.then(() => {
...@@ -568,11 +604,13 @@ Clears this **Preferences** instance. This API uses an asynchronous callback to ...@@ -568,11 +604,13 @@ Clears this **Preferences** instance. This API uses an asynchronous callback to
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------------------------------------------------- | | -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error code.| | callback | AsyncCallback&lt;void&gt; | 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 +631,13 @@ Clears this **Preferences** instance. This API uses a promise to return the resu ...@@ -593,11 +631,13 @@ Clears this **Preferences** instance. This API uses a promise to return the resu
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------- | ------------------------- | | ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promise = preferences.clear() let promise = preferences.clear()
promise.then(() => { promise.then(() => {
...@@ -617,56 +657,14 @@ Subscribes to data changes. A callback will be triggered to return the new value ...@@ -617,56 +657,14 @@ Subscribes to data changes. A callback will be triggered to return the new value
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | -------------------------------- | ---- | ---------------------------------------- | | -------- | -------------------------------- | ---- | ---------------------------------------- |
| type | string | Yes | Event type to subscribe to. The value **change** indicates data change events.| | type | string | Yes | Event type to subscribe to. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt; | Yes | Callback invoked to return data changes. | | callback | Callback&lt;{ key : string }&gt; | Yes | Callback invoked to return data changes. |
**Example** **Example**
```js
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences.");
return;
}
var observer = function (key) {
console.info("The key " + key + " changed.");
}
preferences.on('change', observer);
preferences.put('startup', 'auto', function (err) {
if (err) {
console.info("Failed to put the value of 'startup'. Cause: " + err);
return;
}
console.info("Put the value of 'startup' successfully.");
preferences.flush(function (err) {
if (err) {
console.info("Failed to flush data. Cause: " + err);
return;
}
console.info("Flushed data successfully."); // The observer will be called.
})
})
})
```
### off('change')
off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void
Unsubscribes from data changes.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------- | ---- | ------------------------------------------ |
| type | string | Yes | Event type to unsubscribe from. The value **change** indicates data change events. |
| callback | Callback&lt;{ key : string }&gt; | No | Callback to unregister. If this parameter is left blank, the callbacks used to subscribing to all data changes will be unregistered.|
**Example**
```js ```js
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) { if (err) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册