# @ohos.data.preferences (User Preferences) The **Preferences** module provides APIs for processing data in the form of key-value (KV) pairs, including querying, modifying, and persisting KV pairs. The key is of the string type, and the value can be a number, a string, a Boolean value, or an array of numbers, strings, or Boolean values. The default encryption level of user preferences is EL2, and the persistent files are stored in the corresponding **el2/** directory. If there is no lock screen password, the sendable preferences can be directly accessed after the device is powered on. If there is a lock screen password, the data can be accessed only after at least one successful unlock operation (by PIN, fingerprint, or facial authentication). Avoid direct access to preferences data without the screen unlock operation. For details about how to modify the encryption level, see [Obtaining and Modifying Encryption Levels](../../../application-dev/application-models/application-context-stage.md#obtaining-and-modifying-encryption-levels). > **NOTE** > > - 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. > > - Preferences are not thread-safe and may cause file damage and data loss when used in multi-process scenarios. Do not use preferences in multi-process scenarios. ## Modules to Import ```ts import { preferences } from '@kit.ArkData'; ``` ## Constants **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core | Name | Type| Readable| Writable| Description | | ---------------- | -------- | ---- | ---- | --------------------------------------- | | MAX_KEY_LENGTH | number | Yes | No | Maximum length of a key, which is 1024 bytes. | | MAX_VALUE_LENGTH | number | Yes | No | Maximum value length, which is 16 x 1024 x 1024 bytes.| ## preferences.getPreferences getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void Obtains a **Preferences** instance. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance. | | callback | AsyncCallback<[Preferences](#preferences)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and the **Preferences** instance obtained is returned. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** FA model: ```ts import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let dataPreferences: preferences.Preferences | null = null; preferences.getPreferences(context, 'myStore', (err: BusinessError, val: preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } dataPreferences = val; console.info("Succeeded in getting preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; let dataPreferences: preferences.Preferences | null = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } dataPreferences = val; console.info("Succeeded in getting preferences."); }) } } ``` ## preferences.getPreferences getPreferences(context: Context, name: string): Promise<Preferences> Obtains a **Preferences** instance. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------- | ---- | ----------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance.| **Return value** | Type | Description | | ------------------------------------------ | ---------------------------------- | | Promise<[Preferences](#preferences)> | Promise used to return the **Preferences** instance obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let dataPreferences: preferences.Preferences | null = null; let promise = preferences.getPreferences(context, 'myStore'); promise.then((object: preferences.Preferences) => { dataPreferences = object; console.info("Succeeded in getting preferences."); }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; let dataPreferences: preferences.Preferences | null = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let promise = preferences.getPreferences(this.context, 'myStore'); promise.then((object: preferences.Preferences) => { dataPreferences = object; console.info("Succeeded in getting preferences."); }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.getPreferences10+ getPreferences(context: Context, options: Options, callback: AsyncCallback<Preferences>): void Obtains a **Preferences** instance. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | | callback | AsyncCallback<[Preferences](#preferences)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and the **Preferences** instance obtained is returned. Otherwise, **err** is an error object. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let dataPreferences: preferences.Preferences | null = null; let options: preferences.Options = { name: 'myStore' }; preferences.getPreferences(context, options, (err: BusinessError, val: preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } dataPreferences = val; console.info("Succeeded in getting preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; let dataPreferences: preferences.Preferences | null = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; preferences.getPreferences(this.context, options, (err: BusinessError, val: preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; } dataPreferences = val; console.info("Succeeded in getting preferences."); }) } } ``` ## preferences.getPreferences10+ getPreferences(context: Context, options: Options): Promise<Preferences> Obtains a **Preferences** instance. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | **Return value** | Type | Description | | --------------------------------------- | ---------------------------------- | | Promise<[Preferences](#preferences)> | Promise used to return the **Preferences** instance obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let dataPreferences: preferences.Preferences | null = null; let options: preferences.Options = { name: 'myStore' }; let promise = preferences.getPreferences(context, options); promise.then((object: preferences.Preferences) => { dataPreferences = object; console.info("Succeeded in getting preferences."); }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; let dataPreferences: preferences.Preferences | null = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; let promise = preferences.getPreferences(this.context, options); promise.then((object: preferences.Preferences) => { dataPreferences = object; console.info("Succeeded in getting preferences."); }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.getPreferencesSync10+ getPreferencesSync(context: Context, options: Options): Preferences Obtains a **Preferences** instance. This API returns the result synchronously. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------- | ---- | ------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | **Return value** | Type | Description | | --------------------------- | --------------------- | | [Preferences](#preferences) | **Preferences** instance obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; let context = featureAbility.getContext(); let dataPreferences: preferences.Preferences | null = null; let options: preferences.Options = { name: 'myStore' }; dataPreferences = preferences.getPreferencesSync(context, options); ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; let dataPreferences: preferences.Preferences | null = null; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; dataPreferences = preferences.getPreferencesSync(this.context, options); } } ``` ## preferences.deletePreferences deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void Deletes a **Preferences** instance from the cache. If the **Preferences** instance has a persistent file, the persistent file will also be deleted. This API uses an asynchronous callback to return the result. Avoid using a deleted **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the deleted **Preferences** instance to null. The system will reclaim them in a unified manner. This API cannot be called concurrently with other **preferences** APIs. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | | 15500010 | Failed to delete preferences file. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); preferences.deletePreferences(context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in deleting preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { preferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in deleting preferences."); }) } } ``` ## preferences.deletePreferences deletePreferences(context: Context, name: string): Promise<void> Deletes a **Preferences** instance from the cache. If the **Preferences** instance has a persistent file, the persistent file will also be deleted. This API uses a promise to return the result. Avoid using a deleted **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the deleted **Preferences** instance to null. The system will reclaim them in a unified manner. This API cannot be called concurrently with other **preferences** APIs. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------- | ---- | ----------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance.| **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | | 15500010 | Failed to delete preferences file. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let promise = preferences.deletePreferences(context, 'myStore'); promise.then(() => { console.info("Succeeded in deleting preferences."); }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let promise = preferences.deletePreferences(this.context, 'myStore'); promise.then(() => { console.info("Succeeded in deleting preferences."); }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.deletePreferences10+ deletePreferences(context: Context, options: Options, callback: AsyncCallback<void>): void Deletes a **Preferences** instance from the cache. If the **Preferences** instance has a persistent file, the persistent file will also be deleted. This API uses an asynchronous callback to return the result. Avoid using a deleted **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the deleted **Preferences** instance to null. The system will reclaim them in a unified manner. This API cannot be called concurrently with other **preferences** APIs. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15500010 | Failed to delete preferences file. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let options: preferences.Options = { name: 'myStore' }; preferences.deletePreferences(context, options, (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in deleting preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; preferences.deletePreferences(this.context, options, (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in deleting preferences."); }) } } ``` ## preferences.deletePreferences10+ deletePreferences(context: Context, options: Options): Promise<void> Deletes a **Preferences** instance from the cache. If the **Preferences** instance has a persistent file, the persistent file will also be deleted. This API uses a promise to return the result. Avoid using a deleted **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the deleted **Preferences** instance to null. The system will reclaim them in a unified manner. This API cannot be called concurrently with other **preferences** APIs. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15500010 | Failed to delete preferences file. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let options: preferences.Options = { name: 'myStore' }; let promise = preferences.deletePreferences(context, options); promise.then(() => { console.info("Succeeded in deleting preferences."); }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; let promise = preferences.deletePreferences(this.context, options); promise.then(() => { console.info("Succeeded in deleting preferences."); }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.removePreferencesFromCache removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void Removes a **Preferences** instance from the cache. This API uses an asynchronous callback to return the result. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); preferences.removePreferencesFromCache(context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in removing preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { preferences.removePreferencesFromCache(this.context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in removing preferences."); }) } } ``` ## preferences.removePreferencesFromCache removePreferencesFromCache(context: Context, name: string): Promise<void> Removes a **Preferences** instance from the cache. This API uses a promise to return the result. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------- | ---- | ----------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance.| **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let promise = preferences.removePreferencesFromCache(context, 'myStore'); promise.then(() => { console.info("Succeeded in removing preferences."); }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let promise = preferences.removePreferencesFromCache(this.context, 'myStore'); promise.then(() => { console.info("Succeeded in removing preferences."); }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.removePreferencesFromCacheSync10+ removePreferencesFromCacheSync(context: Context, name: string): void Removes a **Preferences** instance from the cache. This API returns the result synchronously. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------- | ---- | ----------------------- | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md). | | name | string | Yes | Name of the **Preferences** instance.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; let context = featureAbility.getContext(); preferences.removePreferencesFromCacheSync(context, 'myStore'); ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { preferences.removePreferencesFromCacheSync(this.context, 'myStore'); } } ``` ## preferences.removePreferencesFromCache10+ removePreferencesFromCache(context: Context, options: Options, callback: AsyncCallback<void>): void Removes a **Preferences** instance from the cache. This API uses an asynchronous callback to return the result. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let options: preferences.Options = { name: 'myStore' }; preferences.removePreferencesFromCache(context, options, (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in removing preferences."); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; } console.info("Succeeded in removing preferences."); }) } } ``` ## preferences.removePreferencesFromCache10+ removePreferencesFromCache(context: Context, options: Options): Promise<void> Removes a **Preferences** instance from the cache. This API uses a promise to return the result. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; let context = featureAbility.getContext(); let options: preferences.Options = { name: 'myStore' }; let promise = preferences.removePreferencesFromCache(context, options); promise.then(() => { console.info("Succeeded in removing preferences."); }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; let promise = preferences.removePreferencesFromCache(this.context, options); promise.then(() => { console.info("Succeeded in removing preferences."); }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } } ``` ## preferences.removePreferencesFromCacheSync10+ removePreferencesFromCacheSync(context: Context, options: Options):void Removes a **Preferences** instance from the cache. This API returns the result synchronously. After an application calls [getPreferences](#preferencesgetpreferences) for the first time to obtain a **Preferences** instance, the obtained **Preferences** instance is cached. When the application calls [getPreferences](#preferencesgetpreferences) again, the **Preferences** instance will be read from the cache instead of from the persistent file. After this API is called to remove the instance from the cache, calling **getPreferences** again will read data from the persistent file and create a new **Preferences** instance. Avoid using a removed **Preferences** instance to perform data operations, which may cause data inconsistency. Instead, set the removed **Preferences** instance to null. The system will reclaim them in a unified manner. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------- | ---- | ------------------------------------------------------------ | | context | Context | Yes | Application context.
For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).
For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| | options | [Options](#options10) | Yes | Configuration options of the **Preferences** instance. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 801 | Capability not supported. | | 15500000 | Inner error. | | 15501001 | Only supported in stage mode. | | 15501002 | The data group id is not valid. | **Example** FA model: ```ts // Obtain the context. import { featureAbility } from '@kit.AbilityKit'; let context = featureAbility.getContext(); let options: preferences.Options = { name: 'myStore' }; preferences.removePreferencesFromCacheSync(context, options); ``` Stage model: ```ts import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { let options: preferences.Options = { name: 'myStore' }; preferences.removePreferencesFromCacheSync(this.context, options); } } ``` ## Options10+ Represents the configuration of a **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core | Name | Type | Mandatory| Description | | ----------- | ------ | ---- | ------------------------------------------------------------ | | name | string | Yes | Name of the **Preferences** instance. | | dataGroupId | string\|null\|undefined | No | Application group ID, which needs to be obtained from AppGallery. This parameter is not supported currently.
This parameter is optional. A **Preferences** instance will be created in the sandbox path corresponding to the specified **dataGroupId**. If this parameter is not specified, the **Preferences** instance is created in the sandbox directory of the application.
**Model restriction**: This attribute can be used only in the stage model.| ## Preferences Provides APIs for obtaining and modifying the stored data. Before calling any API of **Preferences**, you must obtain a **Preferences** instance by using [preferences.getPreferences](#preferencesgetpreferences). ### get get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void Obtains the value of a key from this **Preferences** instance. This API uses an asynchronous callback to return the result. If the value is null or is not of the default value type, **defValue** is returned. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------------------- | ---- |---------------------------| | key | string | Yes | Key of the data to obtain. It cannot be empty. | | defValue | [ValueType](#valuetype) | Yes | Default value to be returned.| | callback | AsyncCallback<[ValueType](#valuetype)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the value obtained. Otherwise, **err** is an error object. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.get('startup', 'default', (err: BusinessError, val: preferences.ValueType) => { if (err) { console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Obtained the value of 'startup' successfully. val: " + val); }) ``` ### get get(key: string, defValue: ValueType): Promise<ValueType> Obtains the value of a key from this **Preferences** instance. This API uses a promise to return the result. If the value is null or is not of the default value type, **defValue** is returned. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------- | ---- |--------| | key | string | Yes | Key of the data to obtain. It cannot be empty. | | defValue | [ValueType](#valuetype) | Yes | Default value to be returned.| **Return value** | Type | Description | | ----------------------------------- | ----------------------------- | | Promise<[ValueType](#valuetype)> | Promise used to return the value obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.get('startup', 'default'); promise.then((data: preferences.ValueType) => { console.info("Got the value of 'startup'. Data: " + data); }).catch((err: BusinessError) => { console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); }) ``` ### getSync10+ getSync(key: string, defValue: ValueType): ValueType Obtains the value of a key from this **Preferences** instance. This API returns the result synchronously. If the value is null or is not of the default value type, **defValue** is returned. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ----------------------- | ---- |---------------------| | key | string | Yes | Key of the data to obtain. It cannot be empty. | | defValue | [ValueType](#valuetype) | Yes | Default value to be returned.| **Return value** | Type | Description | | ----------------------------------- | ----------------------------- | | [ValueType](#valuetype) | Returns the value obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts let value: preferences.ValueType = dataPreferences.getSync('startup', 'default'); ``` ### getAll getAll(callback: AsyncCallback<Object>): void; Obtains all KV pairs from this **Preferences** instance. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------- | ---- | ------------------------------------------------------------ | | callback | AsyncCallback<Object> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **value** provides all KV pairs obtained. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Mandatory parameters are left unspecified.| | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // There is no Object.keys in ArkTS, and the for..in... syntax cannot be used. // If an error is reported, extract this API to a .ts file and expose it. Then import the API to the .ets file when required. function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; } dataPreferences.getAll((err: BusinessError, value: Object) => { if (err) { console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); return; } let allKeys = getObjKeys(value); console.info("getAll keys = " + allKeys); console.info("getAll object = " + JSON.stringify(value)); }) ``` ### getAll getAll(): Promise<Object> Obtains all KV pairs from this **Preferences** instance. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Return value** | Type | Description | | --------------------- | ------------------------------------------- | | Promise<Object> | Promise used to return the KV pairs obtained.| **Error codes** For details about the error codes, see [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // There is no Object.keys in ArkTS, and the for..in... syntax cannot be used. // If an error is reported, extract this API to a .ts file and expose it. Then import the API to the .ets file when required. function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; } let promise = dataPreferences.getAll(); promise.then((value: Object) => { let allKeys = getObjKeys(value); console.info('getAll keys = ' + allKeys); console.info("getAll object = " + JSON.stringify(value)); }).catch((err: BusinessError) => { console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); }) ``` ### getAllSync10+ getAllSync(): Object Obtains all KV pairs from this **Preferences** instance. This API returns the result synchronously. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Return value** | Type | Description | | --------------------- | ------------------------------------------- | | Object | Returns all KV pairs obtained.| **Error codes** For details about the error codes, see [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 15500000 | Inner error. | **Example** ```ts // There is no Object.keys in ArkTS, and the for..in... syntax cannot be used. // If an error is reported, extract this API to a .ts file and expose it. Then import the API to the .ets file when required. function getObjKeys(obj: Object): string[] { let keys = Object.keys(obj); return keys; } let value = dataPreferences.getAllSync(); let allKeys = getObjKeys(value); console.info('getAll keys = ' + allKeys); console.info("getAll object = " + JSON.stringify(value)); ``` ### put put(key: string, value: ValueType, callback: AsyncCallback<void>): void Writes data to this **Preferences** instance. This API uses an asynchronous callback to return the result. You can use [flush](#flush) to persist the **Preferences** instance. > **NOTE** > > If the key already exists, **put()** overwrites the value. You can use **hasSync()** to check whether the KV pair exists. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- |-------------------------| | key | string | Yes | Key of the data. It cannot be empty.| | value | [ValueType](#valuetype) | Yes | Value to write.| | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.put('startup', 'auto', (err: BusinessError) => { if (err) { console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully put the value of 'startup'."); }) ``` ### put put(key: string, value: ValueType): Promise<void> Writes data to this **Preferences** instance. This API uses a promise to return the result. You can use [flush](#flush) to persist the **Preferences** instance. > **NOTE** > > If the key already exists, **put()** overwrites the value. You can use **hasSync()** to check whether the KV pair exists. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ----------------------- | ---- |--------------------------| | key | string | Yes | Key of the data. It cannot be empty. | | value | [ValueType](#valuetype) | Yes | Value to write.| **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.put('startup', 'auto'); promise.then(() => { console.info("Successfully put the value of 'startup'."); }).catch((err: BusinessError) => { console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); }) ``` ### putSync10+ putSync(key: string, value: ValueType): void Writes data to this **Preferences** instance. This API returns the result synchronously. You can use [flush](#flush) to persist the **Preferences** instance. > **NOTE** > > If the key already exists, **putSync()** overwrites the value. You can use **hasSync()** to check whether the KV pair exists. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ----------------------- | ---- | ------------------------ | | key | string | Yes | Key of the data. It cannot be empty.| | value | [ValueType](#valuetype) | Yes | Value to write.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts dataPreferences.putSync('startup', 'auto'); ``` ### has has(key: string, callback: AsyncCallback<boolean>): void Checks whether this **Preferences** instance contains the KV pair of the given key. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------- | ---- | ------------------------------------------------------------ | | key | string | Yes | Key of the data to check. It cannot be empty. | | callback | AsyncCallback<boolean> | Yes | Callback used to return the result. If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.has('startup', (err: BusinessError, val: boolean) => { if (err) { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); return; } if (val) { console.info("The key 'startup' is contained."); } else { console.info("The key 'startup' is not contained."); } }) ``` ### has has(key: string): Promise<boolean> Checks whether this **Preferences** instance contains the KV pair of the given key. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------- | | key | string | Yes | Key of the data to check. It cannot be empty.| **Return value** | Type | Description | | ---------------------- | ------------------------------------------------------------ | | Promise<boolean> | Promise used to return the result. If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.has('startup'); promise.then((val: boolean) => { if (val) { console.info("The key 'startup' is contained."); } else { console.info("The key 'startup' is not contained."); } }).catch((err: BusinessError) => { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); }) ``` ### hasSync10+ hasSync(key: string): boolean Checks whether this **Preferences** instance contains the KV pair of the given key. This API returns the result synchronously. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------- | | key | string | Yes | Key of the data to check. It cannot be empty.| **Return value** | Type | Description | | ---------------------- | ------------------------------------------------------------ | | boolean | If the **Preferences** instance contains the KV pair, **true** will be returned. Otherwise, **false** will be returned.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts let isExist: boolean = dataPreferences.hasSync('startup'); if (isExist) { console.info("The key 'startup' is contained."); } else { console.info("The key 'startup' is not contained."); } ``` ### delete delete(key: string, callback: AsyncCallback<void>): void Deletes a KV pair from this **Preferences** instance. This API uses an asynchronous callback to return the result. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ---------------------------------------------------- | | key | string | Yes | Key of the KV pair to delete. It cannot be empty. | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.delete('startup', (err: BusinessError) => { if (err) { console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); return; } console.info("Deleted the key 'startup'."); }) ``` ### delete delete(key: string): Promise<void> Deletes a KV pair from this **Preferences** instance. This API uses a promise to return the result. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------- | | key | string | Yes | Key of the KV pair to delete. It cannot be empty.| **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.delete('startup'); promise.then(() => { console.info("Deleted the key 'startup'."); }).catch((err: BusinessError) => { console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); }) ``` ### deleteSync10+ deleteSync(key: string): void Deletes a KV pair from this **Preferences** instance. This API returns the result synchronously. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------- | | key | string | Yes | Key of the KV pair to delete. It cannot be empty.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts dataPreferences.deleteSync('startup'); ``` ### flush flush(callback: AsyncCallback<void>): void Flushes the data in this **Preferences** instance to the persistent file. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ---------------------------------------------------- | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Mandatory parameters are left unspecified. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully flushed data."); }) ``` ### flush flush(): Promise<void> Flushes the data in this **Preferences** instance to the persistent file. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.flush(); promise.then(() => { console.info("Successfully flushed data."); }).catch((err: BusinessError) => { console.error("Failed to flush. code =" + err.code + ", message =" + err.message); }) ``` ### flushSync14+ flushSync(): void Flushes the data in the cached **Preferences** instance to the persistent file. **Atomic service API**: This API can be used in atomic services since API version 14. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Error codes** For details about the error codes, see [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 15500000 | Inner error. | **Example** ```ts dataPreferences.flushSync(); ``` ### clear clear(callback: AsyncCallback<void>): void Clears this **Preferences** instance. This API uses an asynchronous callback to return the result. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ---------------------------------------------------- | | callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Mandatory parameters are left unspecified. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; dataPreferences.clear((err: BusinessError) =>{ if (err) { console.error("Failed to clear. code =" + err.code + ", message =" + err.message); return; } console.info("Successfully cleared data."); }) ``` ### clear clear(): Promise<void> Clears this **Preferences** instance. This API uses a promise to return the result. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Return value** | Type | Description | | ------------------- | ------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let promise = dataPreferences.clear(); promise.then(() => { console.info("Successfully cleared data."); }).catch((err: BusinessError) => { console.error("Failed to clear. code =" + err.code + ", message =" + err.message); }) ``` ### clearSync10+ clearSync(): void Clears this **Preferences** instance. This API returns the result synchronously. You can use [flush](#flush) to persist the **Preferences** instance. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Example** ```ts dataPreferences.clearSync(); ``` ### on('change') on(type: 'change', callback: Callback<string>): void Subscribes to data changes. The registered callback will be invoked to return the new value if the data change is [flushed](#flush). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | -------- | ---- | ---------------------------------------- | | type | string | Yes | Event type. The value is **'change'**, which indicates data changes.| | callback | Callback<string> | Yes | Callback used to return the data change. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (key: string) => { console.info("The key " + key + " changed."); } dataPreferences.on('change', observer); dataPreferences.putSync('startup', 'manual'); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) ``` ### on('multiProcessChange')10+ on(type: 'multiProcessChange', callback: Callback<string>): void Subscribes to inter-process data changes. For the multiple processes holding the same preference file, if the value of the subscribed key changes in any process, the callback in this API will be invoked after [flush()](#flush) is executed. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | -------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type. The value is **'multiProcessChange'**, which indicates inter-process data changes.| | callback | Callback<string> | Yes | Callback used to return the data change. | **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | -------------------------------------- | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | | 15500019 | Failed to obtain subscription service. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (key: string) => { console.info("The key " + key + " changed."); } dataPreferences.on('multiProcessChange', observer); dataPreferences.putSync('startup', 'manual'); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) ``` ### on('dataChange')12+ on(type: 'dataChange', keys: Array<string>, callback: Callback<Record<string, ValueType>>): void Subscribes to changes of specific data. The registered callback will be invoked only after the values of the specified keys are changed and [flushed](#flush). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type. The value is **'dataChange'**, which indicates data changes. | | keys | Array<string> | Yes | Array of the keys to be observed. | | callback | Callback<Record<string, [ValueType](#valuetype)>> | Yes | Callback used to return the changed data, in an array of KV pairs. The keys identify the data changed, and the values are the new values. The values support the following data types: number, string, boolean, Array\, Array\, Array\< boolean>, Uint8Array, and object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (data: Record) => { for (const keyValue of Object.entries(data)) { console.info(`observer : ${keyValue}`) } console.info("The observer called.") } let keys = ['name', 'age'] dataPreferences.on('dataChange', keys, observer); dataPreferences.putSync('name', 'xiaohong'); dataPreferences.putSync('weight', 125); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) ``` ### off('change') off(type: 'change', callback?: Callback<string>): void Unsubscribes from data changes. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | -------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type. The value is **'change'**, which indicates data changes. | | callback | Callback<string> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for data changes.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (key: string) => { console.info("The key " + key + " changed."); } dataPreferences.on('change', observer); dataPreferences.putSync('startup', 'auto'); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) dataPreferences.off('change', observer); ``` ### off('multiProcessChange')10+ off(type: 'multiProcessChange', callback?: Callback<string>): void Unsubscribes from inter-process data changes. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | -------- | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type. The value is **'multiProcessChange'**, which indicates inter-process data changes.| | callback | Callback<string> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for data changes.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (key: string) => { console.info("The key " + key + " changed."); } dataPreferences.on('multiProcessChange', observer); dataPreferences.putSync('startup', 'auto'); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) dataPreferences.off('multiProcessChange', observer); ``` ### off('dataChange')12+ off(type: 'dataChange', keys: Array<string>, callback?: Callback<Record<string, ValueType>>): void Unsubscribes from changes of specific data. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | | type | string | Yes | Event type. The value is **'dataChange'**, which indicates data changes. | | keys | Array<string> | Yes | Array of keys to be unsubscribed from. If this parameter is left empty, all keys are unsubscribed from.| | callback | Callback<Record<string, [ValueType](#valuetype)>> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the changes of the specified data.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [User Preference Error Codes](errorcode-preferences.md). | ID| Error Message | | -------- | ------------------------------ | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified;
2. Incorrect parameter types;
3. Parameter verification failed. | | 15500000 | Inner error. | **Example** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let observer = (data: Record) => { for (const keyValue of Object.entries(data)) { console.info(`observer : ${keyValue}`) } console.info("The observer called.") } let keys = ['name', 'age'] dataPreferences.on('dataChange', keys, observer); dataPreferences.putSync('name', 'xiaohong'); dataPreferences.putSync('weight', 125); dataPreferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Successfully flushed data."); }) dataPreferences.off('dataChange', keys, observer); ``` ## ValueType type ValueType = number | string | boolean | Array\ | Array\ | Array\ | Uint8Array | object | bigint Enumerates the value types. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.DistributedDataManager.Preferences.Core | Type | Description | |--------------------------|-------------------| | number | The value is a number. | | string | The value is a string. | | boolean | The value is true or false. | | Array\ | The value is an array of numbers. | | Array\ | The value is a Boolean array. | | Array\ | The value is an array of strings. | | Uint8Array11+ | The value is an array of 8-bit unsigned integers.| | object12+ | The value is an object.| | bigint12+ | The value is an integer in any format. |