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

!3933 #I55XUT完成,请审批

Merge pull request !3933 from Annie_wang/PR3682
# Lightweight Storage # Preferences
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type. Preferences provide capabilities for processing key-value (KV) data for applications and supports lightweight data persistence, modification, and query. Data is stored in KV pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
``` ```ts
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences';
``` ```
## Attributes ## Constants
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It is 80 bytes.| | MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It is 80 bytes.|
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value of the string type. It is 8192 bytes.| | MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It is 8192 bytes.|
## data_preferences.getPreferences ## data_preferences.getPreferences
getPreferences(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void getPreferences(context: Context, name: string, callback: AsyncCallback&lt;Preferences&gt;): void
Reads a file and loads the data to the **Preferences** instance. This method uses an asynchronous callback to return the execution result. Reads a preference persistence file and loads data to the **Preferences** instance for data operations. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -35,35 +35,37 @@ Reads a file and loads the data to the **Preferences** instance. This method use ...@@ -35,35 +35,37 @@ Reads a file and loads the data to the **Preferences** instance. This method use
- Parameters - Parameters
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| context | Context | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
var path = this.context.getDataBaseDir() export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) { if (err) {
console.info("Get the preferences failed, path: " + path + '/mystore') console.info("Failed to get the preferences")
return; return;
} }
preferences.put('startup', 'auto', function (err) { preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
preferences.flush(function (err) { preferences.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush data to file, err: " + err)
return return
} }
console.info("Flushed to file successfully.") console.info("Flushed data to file successfully.")
}) })
}) })
}) })
}
``` ```
...@@ -71,45 +73,47 @@ Reads a file and loads the data to the **Preferences** instance. This method use ...@@ -71,45 +73,47 @@ Reads a file and loads the data to the **Preferences** instance. This method use
getPreferences(context: Context, name: string): Promise&lt;Preferences&gt; getPreferences(context: Context, name: string): Promise&lt;Preferences&gt;
Reads a file and loads the data to the **Preferences** instance. This method uses a promise to return the execution result. Reads a preference persistence file and loads data to the **Preferences** instance for data operations. 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 | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | 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 execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
var path = this.context.getDataBaseDir() export default class MainAbility extends Ability {
let promisePre = data_preferences.getPreferences(this.context, 'mystore')
promisePre.then((preferences) => { let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
preferences.put('startup', 'auto', function (err) { preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
preferences.flush(function (err) { preferences.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush data to file, err: " + err)
return return
} }
console.info("Flushed to file successfully.") console.info("Flushed data to file successfully.")
}) })
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the preferences failed, path: " + path + '/mystore') console.info("Failed to get the preferences")
}) })
}
``` ```
...@@ -117,28 +121,32 @@ Reads a file and loads the data to the **Preferences** instance. This method use ...@@ -117,28 +121,32 @@ Reads a file and loads the data to the **Preferences** instance. This method use
deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
Removes the singleton **Preferences** instance of the specified file from the memory, and deletes the specified file, its backup file, and corrupted files. After the specified files are deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses an asynchronous callback to return the execution result. Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
Once a preference persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the execution 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 | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.deletePreferences(this.context, 'mystore', function (err) { data_preferences.deletePreferences(this.context, 'mystore', function (err) {
if (err) { if (err) {
console.info("Deleted failed with err: " + err) console.info("Failed to delete data, err: " + err)
return return
} }
console.info("Deleted successfully.") console.info("Deleted data successfully.")
}) })
}
``` ```
...@@ -146,31 +154,35 @@ Removes the singleton **Preferences** instance of the specified file from the me ...@@ -146,31 +154,35 @@ Removes the singleton **Preferences** instance of the specified file from the me
deletePreferences(context: Context, name: string): Promise&lt;void&gt; deletePreferences(context: Context, name: string): Promise&lt;void&gt;
Removes the singleton **Preferences** instance of the specified file from the memory, and deletes the specified file, its backup file, and corrupted files. After the specified files are deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses a promise to return the execution result. Deletes a **Preferences** singleton instance, the persistence file and backup file, and corrupted files from the memory.
Once a preference persistence file is deleted, the **Preferences** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the execution 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 | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | 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 used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
let promisedelPre = data_preferences.deletePreferences(this.context, 'mystore') export default class MainAbility extends Ability {
promisedelPre.then(() => {
console.info("Deleted successfully.") let promise = data_preferences.deletePreferences(this.context, 'mystore')
promise.then(() => {
console.info("Deleted data successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Deleted failed with err: " + err) console.info("Failed to delete data, err: " + err)
}) })
}
``` ```
...@@ -178,30 +190,33 @@ Removes the singleton **Preferences** instance of the specified file from the me ...@@ -178,30 +190,33 @@ Removes the singleton **Preferences** instance of the specified file from the me
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
Removes the singleton **Preferences** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. Removes a **Preferences** singleton instance from the memory.
This method uses an asynchronous callback to return the result. When a **Preferences** singleton instance is removed, this instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the execution 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 | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | name | string | Yes| Name of the **Preferences** instance persistence file.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
if (err) { if (err) {
console.info("Removed preferences from cache failed with err: " + err) console.info("Failed to remove preferences from cache, err: " + err)
return return
} }
console.info("Removed preferences from cache successfully.") console.info("Removed preferences from cache successfully.")
}) })
}
``` ```
...@@ -209,33 +224,36 @@ This method uses an asynchronous callback to return the result. ...@@ -209,33 +224,36 @@ This method uses an asynchronous callback to return the result.
removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt; removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;
Removes the singleton **Preferences** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. Removes a **Preferences** singleton instance from the memory.
This method uses a promise to return the result. When a **Preferences** singleton instance is removed, this instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the execution 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 | Yes| Context of the app or functionality.| | context | [Context](js-apis-Context.md) | Yes| Context of the app or functionality.|
| name | string | Yes| Name of the app's internal data storage.| | 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 used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences' import data_preferences from '@ohos.data.preferences'
let promiserevPre = data_preferences.removePreferencesFromCache(this.context, 'mystore') export default class MainAbility extends Ability {
promiserevPre.then(() => {
let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
promise.then(() => {
console.info("Removed preferences from cache successfully.") console.info("Removed preferences from cache successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Removed preferences from cache failed with err: " + err) console.info("Failed to remove preferences from cache, err: " + err)
}) })
}
``` ```
...@@ -248,28 +266,37 @@ Provides APIs for obtaining and modifying storage data. ...@@ -248,28 +266,37 @@ Provides APIs for obtaining and modifying storage data.
get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. Obtains the value of a key. If the value is null or a non-default value, the default data is returned. This API uses an asynchronous callback to return the result.
This method 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. It cannot be empty.| | key | string | Yes| Key of the data to obtain. It cannot be empty.|
| defValue | 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, string, or Boolean value.|
| callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Get the preferences failed, err: " + err)
return
}
preferences.get('startup', 'default', function(err, value) { preferences.get('startup', 'default', function(err, value) {
if (err) { if (err) {
console.info("Get the value of startup failed with err: " + err) console.info("Get the value of startup failed, err: " + err)
return return
} }
console.info("The value of startup is " + value) console.info("The value of startup is " + value)
}) })
})
}
``` ```
...@@ -277,31 +304,39 @@ This method uses an asynchronous callback to return the result. ...@@ -277,31 +304,39 @@ This method uses an asynchronous callback to return the result.
get(key: string, defValue: ValueType): Promise&lt;ValueType&gt; get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. Obtains the value of a key. If the value is null or a non-default value, the default data is returned. This API uses a promise to return the result.
This method 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. It cannot be empty.| | key | string | Yes| Key of the data to obtain. It cannot be empty.|
| defValue | 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, string, or Boolean value.|
- 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 execution result.|
- Example - Example
``` ```ts
let promiseget = preferences.get('startup', 'default') import Ability from '@ohos.application.Ability'
promiseget.then((value) => { import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promiseGet = preferences.get('startup', 'default')
promiseGet.then((value) => {
console.info("The value of startup is " + value) console.info("The value of startup is " + value)
}).catch((err) => { }).catch((err) => {
console.info("Get the value of startup failed with err: " + err) console.info("Failed to get the value of startup, err: " + err)
}) })
}).catch((err) => {
console.info("Get the preferences failed, err: " + err)
})
}
``` ```
...@@ -309,28 +344,37 @@ This method uses a promise to return the result. ...@@ -309,28 +344,37 @@ This method uses a promise to return the result.
put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void
Obtains the **Preferences** instance corresponding to the specified file, writes data to the **Preferences** instance using a **Preferences** API, and saves data to the file using **flush()** or **flushSync()**. Obtain a **Preferences** instance, writes data to the **Preferences** instance, and saves the data to the file using **flush()** or **flushSync()**. This API uses an asynchronous callback to return the execution result.
This method 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 modify. It cannot be empty.| | key | string | Yes| Key of the data. It cannot be empty.|
| value | 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 used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.put('startup', 'auto', function (err) { preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
}) })
})
}
``` ```
...@@ -338,31 +382,39 @@ This method uses an asynchronous callback to return the result. ...@@ -338,31 +382,39 @@ This method uses an asynchronous callback to return the result.
put(key: string, value: ValueType): Promise&lt;void&gt; put(key: string, value: ValueType): Promise&lt;void&gt;
Obtains the **Preferences** instance corresponding to the specified file, writes data to the **Preferences** instance using a **Preferences** API, and saves data to the file using **flush()** or **flushSync()**. Obtain a **Preferences** instance, writes data to the **Preferences** instance, and saves the data to the file using **flush()** or **flushSync()**. This API uses a promise to return the execution result.
This method 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 modify. It cannot be empty.| | key | string | Yes| Key of the data. It cannot be empty.|
| value | 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.|
- Return value - Return value
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| | Promise&lt;void&gt; | Promise used to return the execution result.|
- Example - Example
``` ```ts
let promiseput = preferences.put('startup', 'auto') import Ability from '@ohos.application.Ability'
promiseput.then(() => { import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promisePut = preferences.put('startup', 'auto')
promisePut.then(() => {
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup, err: " + err)
})
}).catch((err) => {
console.info("Get the preferences failed, err: " + err)
}) })
}
``` ```
...@@ -370,34 +422,45 @@ This method uses a promise to return the result. ...@@ -370,34 +422,45 @@ This method uses a promise to return the result.
has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean
Checks whether the **Preference** object contains data with a given key. Checks whether the **Preferences** instance contains data with a given key. This API uses an asynchronous callback to return the result.
This method 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. 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 execution result.| | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|
- Return value - Return value
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the **Preference** object contains data with the specified key; returns **false** otherwise.| | boolean | Returns **true** if the **Preferences** instance contains data with the specified key; returns **false** otherwise.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.has('startup', function (err, isExist) { preferences.has('startup', function (err, isExist) {
if (err) { if (err) {
console.info("Check the key of startup failed with err: " + err) console.info("Failed to check the key of startup, err: " + err)
return return
} }
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") console.info("The key of startup is contained.")
} else {
console.info("The key of startup is not contained.")
} }
}) })
})
}
``` ```
...@@ -405,32 +468,42 @@ This method uses an asynchronous callback to return the result. ...@@ -405,32 +468,42 @@ This method uses an asynchronous callback to return the result.
has(key: string): Promise&lt;boolean&gt; has(key: string): Promise&lt;boolean&gt;
Checks whether the **Preference** object contains data with a given key. Checks whether the **Preferences** instance contains data with a given key. This API uses a promise to return the result.
This method 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. 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.| | Promise&lt;boolean&gt; | Promise used to return the execution result.|
- Example - Example
``` ```ts
let promisehas = preferences.has('startup') import Ability from '@ohos.application.Ability'
promisehas.then((isExist) => { import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promiseHas = preferences.has('startup')
promiseHas.then((isExist) => {
if (isExist) { if (isExist) {
console.info("The key of startup is contained.") console.info("The key of startup is contained.")
} else {
console.info("The key of startup is not contained.")
} }
}).catch((err) => { }).catch((err) => {
console.info("Check the key of startup failed with err: " + err) console.info("Check the key of startup failed, err: " + err)
})
}).catch((err) => {
console.info("Get the preferences failed, err: " + err)
}) })
}
``` ```
...@@ -438,27 +511,36 @@ This method uses a promise to return the result. ...@@ -438,27 +511,36 @@ This method uses a promise to return the result.
delete(key: string, callback: AsyncCallback&lt;void&gt;): void delete(key: string, callback: AsyncCallback&lt;void&gt;): void
Deletes data with the specified key from this **Preference** object. Deletes the KV pair of the specified key from this **Preferences** instance. This API uses an asynchronous callback to return the result.
This method 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. 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 execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.delete('startup', function (err) { preferences.delete('startup', function (err) {
if (err) { if (err) {
console.info("Delete startup key failed with err: " + err) console.info("Failed to delete startup key, err: " + err)
return return
} }
console.info("Deleted startup key successfully.") console.info("Deleted startup key successfully.")
}) })
})
}
``` ```
...@@ -466,30 +548,38 @@ This method uses an asynchronous callback to return the result. ...@@ -466,30 +548,38 @@ This method uses an asynchronous callback to return the result.
delete(key: string): Promise&lt;void&gt; delete(key: string): Promise&lt;void&gt;
Deletes data with the specified key from this **Preference** object. Deletes the KV pair of the specified key from this **Preferences** instance. This API uses a promise to return the result.
This method 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.| | 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 used to return the execution result.|
- Example - Example
``` ```ts
let promisedel = preferences.delete('startup') import Ability from '@ohos.application.Ability'
promisedel.then(() => { import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promiseDelete = preferences.delete('startup')
promiseDelete.then(() => {
console.info("Deleted startup key successfully.") console.info("Deleted startup key successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Delete startup key failed with err: " + err) console.info("Failed to delete startup key, err: " + err)
})
}).catch((err) => {
console.info("Failed to get the preferences, err: " + err)
}) })
}
``` ```
...@@ -497,9 +587,7 @@ This method uses a promise to return the result. ...@@ -497,9 +587,7 @@ This method uses a promise to return the result.
flush(callback: AsyncCallback&lt;void&gt;): void flush(callback: AsyncCallback&lt;void&gt;): void
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file. Saves the modification to this **Preferences** instance and synchronizes the modification to the preference persistence file. This API uses an asynchronous callback to return the result.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -509,14 +597,25 @@ This method uses an asynchronous callback to return the result. ...@@ -509,14 +597,25 @@ This method uses an asynchronous callback to return the result.
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.flush(function (err) { preferences.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush data to file, err: " + err)
return return
} }
console.info("Flushed to file successfully.") console.info("Flushed data to file successfully.")
})
}) })
}
``` ```
...@@ -524,25 +623,33 @@ This method uses an asynchronous callback to return the result. ...@@ -524,25 +623,33 @@ This method uses an asynchronous callback to return the result.
flush(): Promise&lt;void&gt; flush(): Promise&lt;void&gt;
Saves the modification of this object to the **Preferences** instance and synchronizes the modification to the file. Saves the modification to this **Preferences** instance and synchronizes the modification to the preference persistence file. This API uses a promise to return the result.
This method 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 used to return the result.| | Promise&lt;void&gt; | Promise used to return the execution result.|
- Example - Example
``` ```ts
let promiseflush = preferences.flush() import Ability from '@ohos.application.Ability'
promiseflush.then(() => { import data_preferences from '@ohos.data.preferences'
console.info("Flushed to file successfully.") export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promiseFlush = preferences.flush()
promiseFlush.then(() => {
console.info("Flushed data to file successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush data to file, err: " + err)
}) })
}).catch((err) => {
console.info("Failed to get the preferences, err: " + err)
})
}
``` ```
...@@ -550,9 +657,7 @@ This method uses a promise to return the result. ...@@ -550,9 +657,7 @@ This method uses a promise to return the result.
clear(callback: AsyncCallback&lt;void&gt;): void clear(callback: AsyncCallback&lt;void&gt;): void
Clears this **Preferences** object. Clears data of this **Preferences** instance. This API uses an asynchronous callback to return the result.
This method uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -562,14 +667,25 @@ This method uses an asynchronous callback to return the result. ...@@ -562,14 +667,25 @@ This method uses an asynchronous callback to return the result.
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.| | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.clear(function (err) { preferences.clear(function (err) {
if (err) { if (err) {
console.info("Clear to file failed with err: " + err) console.info("Failed to clear data, err: " + err)
return return
} }
console.info("Cleared to file successfully.") console.info("Cleared to file successfully.")
}) })
})
}
``` ```
...@@ -577,25 +693,33 @@ This method uses an asynchronous callback to return the result. ...@@ -577,25 +693,33 @@ This method uses an asynchronous callback to return the result.
clear(): Promise&lt;void&gt; clear(): Promise&lt;void&gt;
Clears this **Preferences** object. Clears data of this **Preferences** instance. This API uses a promise to return the result.
This method 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 used to return the result.| | Promise&lt;void&gt; | Promise used to return the execution result.|
- Example - Example
``` ```ts
let promiseclear = preferences.clear() import Ability from '@ohos.application.Ability'
promiseclear.then(() => { import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
let promise = data_preferences.getPreferences(this.context, 'mystore')
promise.then((preferences) => {
let promiseClear = preferences.clear()
promiseClear.then(() => {
console.info("Cleared to file successfully.") console.info("Cleared to file successfully.")
}).catch((err) => { }).catch((err) => {
console.info("Clear to file failed with err: " + err) console.info("Failed to clear data, err: " + err)
}) })
}).catch((err) => {
console.info("Failed to get the preferences, err: " + err)
})
}
``` ```
...@@ -603,7 +727,7 @@ This method uses a promise to return the result. ...@@ -603,7 +727,7 @@ This method uses a promise to return the result.
on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void
Subscribes to data changes. When the value of the subscribed key changes, a callback will be invoked after the **flush()** method is executed. Subscribes to data changes. When the value of the subscribed key changes, a callback will be invoked after **flush()** is executed.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core **System capability**: SystemCapability.DistributedDataManager.Preferences.Core
...@@ -614,25 +738,36 @@ Subscribes to data changes. When the value of the subscribed key changes, a call ...@@ -614,25 +738,36 @@ Subscribes to data changes. When the value of the subscribed key changes, a call
| callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.| | callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
var observer = function (key) { var observer = function (key) {
console.info("The key of " + key + " changed.") console.info("The key of " + key + " changed.")
} }
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.on('change', observer) preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) { preferences.put('startup', 'auto', function (err) {
if (err) { if (err) {
console.info("Put the value of startup failed with err: " + err) console.info("Failed to put the value of startup, err: " + err)
return return
} }
console.info("Put the value of startup successfully.") console.info("Put the value of startup successfully.")
preferences.flush(function (err) { preferences.flush(function (err) {
if (err) { if (err) {
console.info("Flush to file failed with err: " + err) console.info("Failed to flush data to file, err: " + err)
return return
} }
console.info("Flushed to file successfully.") // observer will be called. console.info("Flushed data to file successfully.") // The observer will be called.
})
}) })
}) })
}
``` ```
...@@ -651,9 +786,46 @@ Unsubscribes from data changes. ...@@ -651,9 +786,46 @@ Unsubscribes from data changes.
| callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.| | callback | Callback&lt;{ key : string }&gt; | Callback used to return data changes.|
- Example - Example
``` ```ts
import Ability from '@ohos.application.Ability'
import data_preferences from '@ohos.data.preferences'
export default class MainAbility extends Ability {
var observer = function (key) { var observer = function (key) {
console.info("The key of " + key + " changed.") console.info("The key of " + key + " changed.")
} }
data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Failed to get the preferences, err: " + err)
return
}
preferences.on('change', observer)
preferences.put('startup', 'auto', function (err) {
if (err) {
console.info("Failed to put the value of startup, err: " + err)
return
}
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 to file successfully.") // observer will be called.
preferences.off('change', observer) preferences.off('change', observer)
})
})
})
}
``` ```
## ValueType
Enumerates the value types.
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
| Name | Description |
| ------- | -------------------- |
| number | The value is a number. |
| string | The value is a string. |
| boolean | The value is of Boolean type.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册