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

!6732 【翻译完成】#I5CJD2

Merge pull request !6732 from Annie_wang/PR5430
# Preferences Development # Preferences Development
> **NOTE**
>
> This feature is supported since API Version 9. For the versions earlier than API Version 9, use [Lightweight Storage](../reference/apis/js-apis-data-storage.md) APIs.
## When to Use ## When to Use
Preferences are ideal for storing data frequently used by applications, but not for storing a large amount of data or data with frequent changes. The application data is persistently stored on a device in the form of files. Note that the instance accessed by an application contains all data of the file. The data is always loaded to the memory of the device until the application removes it from the memory. The application can perform data operations using the **Preferences** APIs. Preferences are ideal for storing data frequently used by applications, but not for storing a large amount of data or data with frequent changes. The application data is persistently stored on a device in the form of files. Note that the instance accessed by an application contains all data of the file. The data is always loaded to the memory of the device until the application removes it from the memory. The application can perform data operations using the **Preferences** APIs.
## Available APIs ## Available APIs
Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type. Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type. For more APIs related to preferences, see [Preferences](../reference/apis/js-apis-data-preferences.md).
### Creating a Preferences Instance ### Creating a Preferences Instance
...@@ -26,7 +30,7 @@ Call the **put()** method to add or modify data in a **Preferences** instance. ...@@ -26,7 +30,7 @@ Call the **put()** method to add or modify data in a **Preferences** instance.
| Class | API | Description | | Class | API | Description |
| ------- | -------------------------------------------------- | ----------------------------------------------- | | ------- | -------------------------------------------------- | ----------------------------------------------- |
| Preferences | put(key: string, value: ValueType): Promise\<void> | Writes data of the number, string, and Boolean types.| | Preferences | put(key: string, value: ValueType): Promise\<void> | Writes data with the value type of number, string, boolean, Array\<number>, Array\<string>, or Array\<boolean>.|
### Reading Data ### Reading Data
...@@ -36,7 +40,7 @@ Call the **get()** method to read data from a **Preferences** instance. ...@@ -36,7 +40,7 @@ Call the **get()** method to read data from a **Preferences** instance.
| Class | API | Description | | Class | API | Description |
| ------- | ---------------------------------------------------------- | ----------------------------------------------- | | ------- | ---------------------------------------------------------- | ----------------------------------------------- |
| Preferences | get(key: string, defValue: ValueType): Promise\<ValueType> | Reads data of the number, string, and Boolean types.| | Preferences | get(key: string, defValue: ValueType): Promise\<ValueType> | Obtains data with the value type of number, string, boolean, Array\<number>, Array\<string>, or Array\<boolean>.|
### Storing Data Persistently ### Storing Data Persistently
...@@ -67,7 +71,7 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -67,7 +71,7 @@ Use the following APIs to delete a **Preferences** instance or data file.
| Package | API | Description | | Package | API | Description |
| ----------------- | ---------------------------------------------------- | ------------------------------------------------------------ | | ----------------- | ---------------------------------------------------- | ------------------------------------------------------------ |
| ohos.data.preferences | deletePreferences(context: Context, name: string): Promise<void>; | Deletes a **Preferences** instance from the cache and deletes its file from the device.| | ohos.data.preferences | deletePreferences(context: Context, name: string): Promise\<void>; | Deletes a **Preferences** instance from the cache and deletes its file from the device.|
| ohos.data.preferences | removePreferencesFromCache(context: Context, name: string): Promise\<void>; | Removes a **Preferences** instance from the memory to release memory. | ohos.data.preferences | removePreferencesFromCache(context: Context, name: string): Promise\<void>; | Removes a **Preferences** instance from the memory to release memory.
## How to Develop ## How to Develop
...@@ -91,8 +95,8 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -91,8 +95,8 @@ Use the following APIs to delete a **Preferences** instance or data file.
```js ```js
promise.then((preferences) => { promise.then((preferences) => {
let getPromise = preferences.put('startup', 'auto') let putPromise = preferences.put('startup', 'auto')
getPromise.then(() => { putPromise.then(() => {
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Failed to put the value of startup with err: " + err) console.info("Failed to put the value of startup with err: " + err)
......
# Preferences # Preferences
Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support lightweight data persistence, modification, and query. In KV pairs, keys are of the string type, and values can be of the number, string, or Boolean type. Preferences provide capabilities for processing data in the form of key-value (KV) pairs and support lightweight data persistence, modification, and query. In KV pairs, the keys are of the string type, and the values can be of the number, string, Boolean, Array\<number>, Array\<string>, or Array\<boolean> type.
> **NOTE**<br/> > **NOTE**<br/>
...@@ -33,11 +33,11 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i ...@@ -33,11 +33,11 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the result.|
**Example** **Example**
```ts ```ts
...@@ -60,15 +60,15 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i ...@@ -60,15 +60,15 @@ Reads a **Preferences** persistence file and loads data to the **Preferences** i
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the result.| | Promise&lt;[Preferences](#preferences)&gt; | Promise used to return the result.|
**Example** **Example**
```ts ```ts
...@@ -91,11 +91,11 @@ Once a **Preferences** persistence file is deleted, the **Preferences** instance ...@@ -91,11 +91,11 @@ Once a **Preferences** persistence file is deleted, the **Preferences** instance
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -119,15 +119,15 @@ Once a **Preferences** persistence file is deleted, the **Preferences** instance ...@@ -119,15 +119,15 @@ Once a **Preferences** persistence file is deleted, the **Preferences** instance
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -151,11 +151,11 @@ When a **Preferences** singleton instance is removed, this instance cannot be us ...@@ -151,11 +151,11 @@ When a **Preferences** singleton instance is removed, this instance cannot be us
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -180,15 +180,15 @@ When a **Preferences** singleton instance is removed, this instance cannot be us ...@@ -180,15 +180,15 @@ When a **Preferences** singleton instance is removed, this instance cannot be us
**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| Context of the application or functionality.| | context | [Context](js-apis-ability-context.md) | Yes| Context of the application or function.|
| name | string | Yes| Name of the **Preferences** instance persistence file.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -218,7 +218,7 @@ Obtains the value of a key. If the value is null or a non-default value, the def ...@@ -218,7 +218,7 @@ Obtains the value of a key. If the value is null or a non-default value, the def
| 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.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.| | defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the result.|
**Example** **Example**
...@@ -241,16 +241,16 @@ Obtains the value of a key. If the value is null or a non-default value, the def ...@@ -241,16 +241,16 @@ Obtains the value of a key. If the value is null or a non-default value, the def
**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.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.| | defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;ValueType&gt; | Promise used to return the result.| | Promise&lt;ValueType&gt; | Promise used to return the result.|
**Example** **Example**
```ts ```ts
...@@ -271,13 +271,13 @@ Obtains the **Object** instance that contains all KV pairs. ...@@ -271,13 +271,13 @@ Obtains the **Object** instance that contains all KV pairs.
**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 used to return the **Object** instance obtained.| | callback | AsyncCallback&lt;Object&gt; | Yes| Callback used to return the **Object** instance obtained.|
**Example** **Example**
```ts ```ts
preferences.get.getAll(function (err, value) { preferences.getAll(function (err, value) {
if (err) { if (err) {
console.info("getAll failed, err: " + err) console.info("getAll failed, err: " + err)
return return
...@@ -298,9 +298,9 @@ Obtains the **Object** instance that contains all KV pairs. ...@@ -298,9 +298,9 @@ Obtains the **Object** instance that contains all KV pairs.
**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**
```ts ```ts
...@@ -323,11 +323,11 @@ Puts a new value to this **Preferences** instance and its persistence file. This ...@@ -323,11 +323,11 @@ Puts a new value to this **Preferences** instance and its persistence file. This
**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| New value to store. It can be a number, string, or Boolean value.| | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -350,15 +350,15 @@ Puts a new value to this **Preferences** instance and its persistence file. This ...@@ -350,15 +350,15 @@ Puts a new value to this **Preferences** instance and its persistence file. This
**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| New value to store. It can be a number, string, or Boolean value.| | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, a string, a Boolean value, an array of numbers, an array of strings, or a Boolean array.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -380,10 +380,10 @@ Checks whether this **Preferences** instance contains data with a given key. Thi ...@@ -380,10 +380,10 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
**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 used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.| | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
**Example** **Example**
```ts ```ts
...@@ -410,14 +410,14 @@ Checks whether this **Preferences** instance contains data with a given key. Thi ...@@ -410,14 +410,14 @@ Checks whether this **Preferences** instance contains data with a given key. Thi
**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. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.| | Promise&lt;boolean&gt; | Promise used to return the result. It returns **true** if the **Preferences** instance contains data with the given key and returns **false** otherwise.|
**Example** **Example**
```ts ```ts
...@@ -443,10 +443,10 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This ...@@ -443,10 +443,10 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This
**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 used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -469,14 +469,14 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This ...@@ -469,14 +469,14 @@ Deletes a KV pair of the specified key from this **Preferences** instance. This
**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 used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -498,9 +498,9 @@ Saves the modification to this **Preferences** instance and synchronizes the mod ...@@ -498,9 +498,9 @@ Saves the modification to this **Preferences** instance and synchronizes the mod
**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 used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -523,9 +523,9 @@ Saves the modification to this **Preferences** instance and synchronizes the mod ...@@ -523,9 +523,9 @@ Saves the modification to this **Preferences** instance and synchronizes the mod
**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 used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -547,9 +547,9 @@ Clears data of this **Preferences** instance. This API uses an asynchronous call ...@@ -547,9 +547,9 @@ Clears data of 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|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -572,9 +572,9 @@ Clears data of this **Preferences** instance. This API uses a promise to return ...@@ -572,9 +572,9 @@ Clears data of this **Preferences** instance. This API uses a promise to return
**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 used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```ts ```ts
...@@ -596,33 +596,36 @@ Subscribes to data changes. When the value of the subscribed key changes, a call ...@@ -596,33 +596,36 @@ Subscribes to data changes. When the value of the subscribed key changes, a call
**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. The value **change** indicates data change events.| | type | string | Yes| Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt; | Yes| Callback used to return data changes.| | callback | Callback&lt;{ key : string }&gt; | Yes| Callback used to return data changes.|
**Example** **Example**
```ts ```ts
var observer = function (key) { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
console.info("The key of " + key + " changed.")
}
...
preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Failed to put the value of startup, err: " + err) console.info("Failed to get preferences.")
return return;
} }
console.info("Put the value of startup successfully.") var observer = function (key) {
console.info("The key of " + key + " changed.")
preferences.flush(function (err) { }
preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Failed to flush data to file, err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Flushed data to file successfully.") // The observer will be called. console.info("Put the value of startup successfully.")
preferences.flush(function (err) {
if (err) {
console.info("Failed to flush data to file, err: " + err)
return
}
console.info("Flushed data to file successfully.") // The observer will be called.
})
}) })
}) })
``` ```
...@@ -637,33 +640,36 @@ Unsubscribes from data changes. ...@@ -637,33 +640,36 @@ 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. The value **change** indicates data change events.| | type | string | Yes| Event type. The value **change** indicates data change events.|
| callback | Callback&lt;{ key : string }&gt; | No| Callback used to return data changes. If this parameter is left empty, all callbacks for data changes will be canceled.| | callback | Callback&lt;{ key : string }&gt; | No| Callback used to return data changes. If this parameter is left empty, all callbacks for data changes will be canceled.|
**Example** **Example**
```ts ```ts
var observer = function (key) { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
console.info("The key of " + key + " changed.")
}
...
preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Failed to put the value of startup, err: " + err) console.info("Failed to get preferences.")
return return;
} }
console.info("Put the value of startup successfully.") var observer = function (key) {
console.info("The key of " + key + " changed.")
preferences.flush(function (err) { }
preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Failed to flush data to file, err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Flushed data to file successfully.") // The observer will be called. console.info("Put the value of startup successfully.")
preferences.flush(function (err) {
if (err) {
console.info("Failed to flush data to file, err: " + err)
return
}
console.info("Flushed data to file successfully.") // The observer will be called.
})
preferences.off('change', observer) preferences.off('change', observer)
}) })
}) })
...@@ -675,8 +681,11 @@ Enumerates the value types. ...@@ -675,8 +681,11 @@ Enumerates the value types.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name | Description | | Name | Description |
| ------- | -------------------- | | -------------- | ------------------------------ |
| number | The value is a number. | | number | The value is a number. |
| string | The value is a string. | | string | The value is a string. |
| boolean | The value is of Boolean type.| | boolean | The value is of Boolean type. |
| Array\<number> | The value is an array of numbers. |
| Array\<boolean> | The value is a Boolean array. |
| Array\<string> | The value is an array of the strings.|
...@@ -13,7 +13,7 @@ Lightweight storage provides applications with data processing capability and al ...@@ -13,7 +13,7 @@ Lightweight storage provides applications with data processing capability and al
## Modules to Import ## Modules to Import
```js ```js
import dataStorage from '@ohos.data.storage'; import data_storage from '@ohos.data.storage';
``` ```
## Constants ## Constants
...@@ -26,7 +26,7 @@ import dataStorage from '@ohos.data.storage'; ...@@ -26,7 +26,7 @@ import dataStorage from '@ohos.data.storage';
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.| | MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.|
## dataStorage.getStorageSync ## data_storage.getStorageSync
getStorageSync(path: string): Storage getStorageSync(path: string): Storage
...@@ -46,24 +46,17 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -46,24 +46,17 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
let path = '/data/storage/el2/database'
let storage = data_storage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()
var context = featureAbility.getContext()
context.getFilesDir((err, path) => {
if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err));
return;
}
console.info('getFilesDir successful. path:' + JSON.stringify(path));
let storage = dataStorage.getStorageSync(path + '/mystore')
storage.putSync('startup', 'auto')
storage.flushSync()
});
``` ```
## dataStorage.getStorage ## data_storage.getStorage
getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void
...@@ -79,29 +72,21 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -79,29 +72,21 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let path = '/data/storage/el2/database'
context.getFilesDir((err, path) => { data_storage.getStorage(path + '/mystore', function (err, storage) {
if (err) { if (err) {
console.error('getFilesDir failed. err: ' + JSON.stringify(err)); console.info("Failed to get the storage. Path: " + path + '/mystore')
return; return;
} }
console.info('getFilesDir successful. path:' + JSON.stringify(path)); storage.putSync('startup', 'auto')
dataStorage.getStorage(path + '/mystore', function (err, storage) { storage.flushSync()
if (err) { })
console.info("Get the storage failed, path: " + path + '/mystore')
return;
}
storage.putSync('startup', 'auto')
storage.flushSync()
})
});
``` ```
## dataStorage.getStorage ## data_storage.getStorage
getStorage(path: string): Promise&lt;Storage&gt; getStorage(path: string): Promise&lt;Storage&gt;
...@@ -121,28 +106,21 @@ Reads the specified file and loads its data to the **Storage** instance for data ...@@ -121,28 +106,21 @@ Reads the specified file and loads its data to the **Storage** instance for data
**Example** **Example**
```js ```js
import dataStorage from '@ohos.data.storage' import data_storage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let path = '/data/storage/el2/database'
context.getFilesDir((err, path) => {
if (err) { let getPromise = data_storage.getStorage(path + '/mystore')
console.info("Get the storage failed, path: " + path + '/mystore') getPromise.then((storage) => {
return; storage.putSync('startup', 'auto')
} storage.flushSync()
console.info('getFilesDir successful. path:' + JSON.stringify(path)); }).catch((err) => {
let promisegetSt = dataStorage.getStorage(path + '/mystore') console.info("Failed to get the storage. Path: " + path + '/mystore')
promisegetSt.then((storage) => { })
storage.putSync('startup', 'auto')
storage.flushSync()
}).catch((err) => {
console.info("Get the storage failed, path: " + path + '/mystore')
})
});
``` ```
## dataStorage.deleteStorageSync ## data_storage.deleteStorageSync
deleteStorageSync(path: string): void deleteStorageSync(path: string): void
...@@ -157,11 +135,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -157,11 +135,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**Example** **Example**
```js ```js
dataStorage.deleteStorageSync(path + '/mystore') let path = '/data/storage/el2/database'
data_storage.deleteStorageSync(path + '/mystore')
``` ```
## dataStorage.deleteStorage ## data_storage.deleteStorage
deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -173,11 +152,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -173,11 +152,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.| | path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
dataStorage.deleteStorage(path + '/mystore', function (err) { let path = '/data/storage/el2/database'
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Deleted failed with err: " + err) console.info("Deleted failed with err: " + err)
return return
...@@ -187,7 +167,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -187,7 +167,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
``` ```
## dataStorage.deleteStorage ## data_storage.deleteStorage
deleteStorage(path: string): Promise&lt;void&gt; deleteStorage(path: string): Promise&lt;void&gt;
...@@ -203,11 +183,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -203,11 +183,12 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promisedelSt = dataStorage.deleteStorage(path + '/mystore') let path = '/data/storage/el2/database'
let promisedelSt = data_storage.deleteStorage(path + '/mystore')
promisedelSt.then(() => { promisedelSt.then(() => {
console.info("Deleted successfully.") console.info("Deleted successfully.")
}).catch((err) => { }).catch((err) => {
...@@ -216,7 +197,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete ...@@ -216,7 +197,7 @@ Deletes the singleton **Storage** instance of a file from the memory, and delete
``` ```
## dataStorage.removeStorageFromCacheSync ## data_storage.removeStorageFromCacheSync
removeStorageFromCacheSync(path: string): void removeStorageFromCacheSync(path: string): void
...@@ -231,11 +212,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -231,11 +212,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**Example** **Example**
```js ```js
dataStorage.removeStorageFromCacheSync(path + '/mystore') let path = '/data/storage/el2/database'
data_storage.removeStorageFromCacheSync(path + '/mystore')
``` ```
## dataStorage.removeStorageFromCache ## data_storage.removeStorageFromCache
removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -247,11 +229,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -247,11 +229,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| path | string | Yes| Path of the target file.| | path | string | Yes| Path of the target file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
dataStorage.removeStorageFromCache(path + '/mystore', function (err) { let path = '/data/storage/el2/database'
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) { if (err) {
console.info("Removed storage from cache failed with err: " + err) console.info("Removed storage from cache failed with err: " + err)
return return
...@@ -261,7 +244,7 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -261,7 +244,7 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
``` ```
## dataStorage.removeStorageFromCache ## data_storage.removeStorageFromCache
removeStorageFromCache(path: string): Promise&lt;void&gt; removeStorageFromCache(path: string): Promise&lt;void&gt;
...@@ -277,11 +260,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed ...@@ -277,11 +260,12 @@ Removes the singleton **Storage** instance of a file from the cache. The removed
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
let promiserevSt = dataStorage.removeStorageFromCache(path + '/mystore') let path = '/data/storage/el2/database'
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => { promiserevSt.then(() => {
console.info("Removed storage from cache successfully.") console.info("Removed storage from cache successfully.")
}).catch((err) => { }).catch((err) => {
...@@ -412,7 +396,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -412,7 +396,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| 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| New value to store. It can be a number, string, or Boolean value.| | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -443,7 +427,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat ...@@ -443,7 +427,7 @@ Obtains the **Storage** instance corresponding to the specified file, writes dat
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -578,7 +562,7 @@ Deletes data with the specified key from this storage object. This API uses an a ...@@ -578,7 +562,7 @@ Deletes data with the specified key from this storage object. This API uses an a
| 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.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -608,7 +592,7 @@ Deletes data with the specified key from this storage object. This API uses a pr ...@@ -608,7 +592,7 @@ Deletes data with the specified key from this storage object. This API uses a pr
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -646,7 +630,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -646,7 +630,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -671,7 +655,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz ...@@ -671,7 +655,7 @@ Saves the modification of this object to the **Storage** instance and synchroniz
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
...@@ -709,7 +693,7 @@ Clears this **Storage** object. This API uses an asynchronous callback to return ...@@ -709,7 +693,7 @@ Clears this **Storage** object. This API uses an asynchronous callback to return
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
**Example** **Example**
```js ```js
...@@ -734,7 +718,7 @@ Clears this **Storage** object. This API uses a promise to return the result. ...@@ -734,7 +718,7 @@ Clears this **Storage** object. This API uses a promise to return the result.
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise that returns no value.|
**Example** **Example**
```js ```js
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册