提交 d67951d2 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 cbfc9332
...@@ -88,15 +88,22 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -88,15 +88,22 @@ Use the following APIs to delete a **Preferences** instance or data file.
2. Obtain a **Preferences** instance. 2. Obtain a **Preferences** instance.
Read the specified file and load its data to the **Preferences** instance for data operations. Read the specified file and load its data to the **Preferences** instance for data operations.
FA model: FA model:
```js ```js
// Obtain the context. // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let context = featureAbility.getContext();
let preferences = null;
let promise = data_preferences.getPreferences(context, 'mystore'); let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((pref) => {
preferences = pref;
}).catch((err) => {
console.info("Failed to get the preferences.");
})
``` ```
Stage model: Stage model:
...@@ -104,14 +111,21 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -104,14 +111,21 @@ Use the following APIs to delete a **Preferences** instance or data file.
```ts ```ts
// Obtain the context. // Obtain the context.
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
var context let context = null;
class MainAbility extends Ability{ let preferences = null;
export default class MainAbility extends Ability {
onWindowStageCreate(windowStage){ onWindowStageCreate(windowStage){
context = this.context context = this.context;
} }
} }
let promise = data_preferences.getPreferences(context, 'mystore'); let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((pref) => {
preferences = pref;
}).catch((err) => {
console.info("Failed to get the preferences.");
})
``` ```
3. Write data. 3. Write data.
...@@ -119,35 +133,27 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -119,35 +133,27 @@ Use the following APIs to delete a **Preferences** instance or data file.
Use the **preferences.put()** method to write data to the **Preferences** instance. Use the **preferences.put()** method to write data to the **Preferences** instance.
```js ```js
promise.then((preferences) => { let putPromise = preferences.put('startup', 'auto');
let putPromise = preferences.put('startup', 'auto'); putPromise.then(() => {
putPromise.then(() => { console.info("Put the value of 'startup' successfully.");
console.info("Put the value of 'startup' successfully.");
}).catch((err) => {
console.info("Failed to put the value of 'startup'. Cause: " + err);
})
}).catch((err) => { }).catch((err) => {
console.info("Failed to get the preferences."); console.info("Failed to put the value of 'startup'. Cause: " + err);
}) })
``` ```
4. Read data. 4. Read data.
Use the **preferences.get()** method to read data. Use the **preferences.get()** method to read data.
```js ```js
promise.then((preferences) => { let getPromise = preferences.get('startup', 'default');
let getPromise = preferences.get('startup', 'default'); getPromise.then((value) => {
getPromise.then((value) => {
console.info("The value of 'startup' is " + value); console.info("The value of 'startup' is " + value);
}).catch((err) => {
console.info("Failed to get the value of 'startup'. Cause: " + err);
})
}).catch((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. 5. Store data persistently.
Use the **flush()** method to flush data from the **Preferences** instance to its file. 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. ...@@ -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
var observer = function (key) { let observer = 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);
return; return;
......
...@@ -38,15 +38,21 @@ Obtains a **Preferences** instance. This API uses an asynchronous callback to re ...@@ -38,15 +38,21 @@ 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 | 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&lt;[Preferences](#preferences)&gt; | 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&lt;[Preferences](#preferences)&gt; | 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.|
**Example** **Example**
FA model:
```js ```js
var preferences = null; // Obtain the context.
data_preferences.getPreferences(this.context, 'mystore', function (err, object) { import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let preferences = null;
data_preferences.getPreferences(context, 'mystore', function (err, object) {
if (err) { if (err) {
console.info("Failed to get the preferences. Cause: " + err); console.info("Failed to get the preferences. Cause: " + err);
return; return;
...@@ -56,6 +62,28 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, object) ...@@ -56,6 +62,28 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, object)
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
let preferences = null;
data_preferences.getPreferences(context, 'mystore', function (err, object) {
if (err) {
console.info("Failed to get the preferences. Cause: " + err);
return;
}
preferences = object;
console.info("Got the preferences successfully.");
})
```
## data_preferences.getPreferences ## data_preferences.getPreferences
...@@ -66,21 +94,29 @@ Obtains a **Preferences** instance. This API uses a promise to return the result ...@@ -66,21 +94,29 @@ 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 | 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.|
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------------------------------------------ | ---------------------------------- | | ------------------------------------------ | ---------------------------------- |
| Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the **Preferences** instance obtained.| | Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the **Preferences** instance obtained.|
**Example** **Example**
FA model:
```js ```js
var preferences = null; // Obtain the context.
let promise = data_preferences.getPreferences(this.context, 'mystore') import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let preferences = null;
let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((object) => { promise.then((object) => {
preferences = object; preferences = object;
console.info("Got the preferences successfully."); console.info("Got the preferences successfully.");
...@@ -89,6 +125,27 @@ promise.then((object) => { ...@@ -89,6 +125,27 @@ promise.then((object) => {
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
let preferences = null;
let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((object) => {
preferences = object;
console.info("Got the preferences successfully.");
}).catch((err) => {
console.info("Failed to get the preferences. Cause: " + err);
})
```
## data_preferences.deletePreferences ## data_preferences.deletePreferences
...@@ -103,23 +160,51 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi ...@@ -103,23 +160,51 @@ 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 | 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&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**
FA model:
```js ```js
data_preferences.deletePreferences(this.context, 'mystore', function (err) { // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
data_preferences.deletePreferences(context, 'mystore', function (err) {
if (err) { if (err) {
console.info("Failed to delete the preferences. Cause: " + err); console.info("Failed to delete the preferences. Cause: " + err);
return return;
} }
console.info("Deleted the preferences successfully." ); console.info("Deleted the preferences successfully." );
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
data_preferences.deletePreferences(context, 'mystore', function (err) {
if (err) {
console.info("Failed to delete the preferences. Cause: " + err);
return;
}
console.info("Deleted the preferences successfully." );
})
```
## data_preferences.deletePreferences ## data_preferences.deletePreferences
...@@ -134,19 +219,28 @@ The deleted **Preferences** instance cannot be used for data operations. Otherwi ...@@ -134,19 +219,28 @@ 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 | 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.|
**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**
FA model:
```js ```js
let promise = data_preferences.deletePreferences(this.context, 'mystore') // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let promise = data_preferences.deletePreferences(context, 'mystore');
promise.then(() => { promise.then(() => {
console.info("Deleted the preferences successfully."); console.info("Deleted the preferences successfully.");
}).catch((err) => { }).catch((err) => {
...@@ -154,6 +248,25 @@ promise.then(() => { ...@@ -154,6 +248,25 @@ promise.then(() => {
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
let promise = data_preferences.deletePreferences(context, 'mystore');
promise.then(() => {
console.info("Deleted the preferences successfully.");
}).catch((err) => {
console.info("Failed to delete the preferences. Cause: " + err);
})
```
## data_preferences.removePreferencesFromCache ## data_preferences.removePreferencesFromCache
...@@ -166,15 +279,23 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi ...@@ -166,15 +279,23 @@ 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 | 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&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**
FA model:
```js ```js
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
data_preferences.removePreferencesFromCache(context, 'mystore', function (err) {
if (err) { if (err) {
console.info("Failed to remove the preferences. Cause: " + err); console.info("Failed to remove the preferences. Cause: " + err);
return; return;
...@@ -183,6 +304,26 @@ data_preferences.removePreferencesFromCache(this.context, 'mystore', function (e ...@@ -183,6 +304,26 @@ data_preferences.removePreferencesFromCache(this.context, 'mystore', function (e
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
data_preferences.removePreferencesFromCache(context, 'mystore', function (err) {
if (err) {
console.info("Failed to remove the preferences. Cause: " + err);
return;
}
console.info("Removed the preferences successfully.");
})
```
## data_preferences.removePreferencesFromCache ## data_preferences.removePreferencesFromCache
...@@ -195,19 +336,28 @@ The removed **Preferences** instance cannot be used for data operations. Otherwi ...@@ -195,19 +336,28 @@ 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 | 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.|
**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**
FA model:
```js ```js
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore') // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let promise = data_preferences.removePreferencesFromCache(context, 'mystore');
promise.then(() => { promise.then(() => {
console.info("Removed the preferences successfully."); console.info("Removed the preferences successfully.");
}).catch((err) => { }).catch((err) => {
...@@ -215,6 +365,25 @@ promise.then(() => { ...@@ -215,6 +365,25 @@ promise.then(() => {
}) })
``` ```
Stage model:
```ts
// Obtain the context.
import Ability from '@ohos.application.Ability';
let context = null;
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context;
}
}
let promise = data_preferences.removePreferencesFromCache(context, 'mystore');
promise.then(() => {
console.info("Removed the preferences successfully.");
}).catch((err) => {
console.info("Failed to remove the preferences. Cause: " + err);
})
```
## Preferences ## Preferences
...@@ -232,6 +401,7 @@ Obtains the value of a key. This API uses an asynchronous callback to return the ...@@ -232,6 +401,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 +430,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the ...@@ -260,6 +430,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 +443,7 @@ Obtains the value of a key. This API uses a promise to return the result. If the ...@@ -272,6 +443,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 +462,7 @@ Obtains an **Object** instance that contains all KV pairs. This API uses an asyn ...@@ -290,6 +462,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 +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
**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 +518,7 @@ Writes data to this **Preferences** instance. This API uses an asynchronous call ...@@ -343,6 +518,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 +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&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 +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
**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(() => {
...@@ -395,17 +575,19 @@ promise.then(() => { ...@@ -395,17 +575,19 @@ promise.then(() => {
has(key: string, callback: AsyncCallback&lt;boolean&gt;): void has(key: string, callback: AsyncCallback&lt;boolean&gt;): void
Checks whether this **Preferences** instance contains a KV pair of the given key. This API uses an asynchronous callback to return the result.. Checks whether this **Preferences** instance contains a KV pair with the given key. This API uses an 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 |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | | -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
| 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) {
...@@ -425,16 +607,18 @@ preferences.has('startup', function (err, isExist) { ...@@ -425,16 +607,18 @@ preferences.has('startup', function (err, isExist) {
has(key: string): Promise&lt;boolean&gt; has(key: string): Promise&lt;boolean&gt;
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..
**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 +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
**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 +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
**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 +703,18 @@ promise.then(() => { ...@@ -514,16 +703,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. Saves the data of this **Preferences** instance to a file asynchronously. This API uses an 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 +730,18 @@ preferences.flush(function (err) { ...@@ -539,16 +730,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. Saves the data of this **Preferences** instance to a file asynchronously. 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 +761,13 @@ Clears this **Preferences** instance. This API uses an asynchronous callback to ...@@ -568,11 +761,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 +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
**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,23 +814,25 @@ Subscribes to data changes. A callback will be triggered to return the new value ...@@ -617,23 +814,25 @@ 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 ```js
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) { if (err) {
console.info("Failed to get the preferences."); console.info("Failed to get the preferences.");
return; return;
} }
var observer = function (key) { let observer = 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) { 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);
return; return;
...@@ -661,19 +860,21 @@ Unsubscribes from data changes. ...@@ -661,19 +860,21 @@ Unsubscribes from data changes.
**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 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&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.| | 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** **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) {
console.info("Failed to get preferences."); console.info("Failed to get the preferences.");
return; return;
} }
var observer = function (key) { let observer = function (key) {
console.info("The key " + key + " changed."); console.info("The key " + key + " changed.");
} }
preferences.on('change', observer); preferences.on('change', observer);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册