1e41f4b71Sopenharmony_ci# @ohos.data.preferences (用户首选项) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci用户首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci用户首选项默认加密等级为EL2,持久化文件存储在对应的EL2路径下。设备开机后,若无锁屏密码,可直接访问;若有锁屏密码,此路径下的文件需要至少一次解锁对应用户的锁屏界面后(密码、指纹、人脸等方式解锁皆可)才能够访问。需避免在开机未解锁的情况下访问首选项。修改加密等级的方法请参见[获取和修改加密分区](../../../application-dev/application-models/application-context-stage.md#获取和修改加密分区)。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci> **说明:** 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> 首选项无法保证进程并发安全,会有文件损坏和数据丢失的风险,不支持在多进程场景下使用。 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 导入模块 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ciimport { preferences } from '@kit.ArkData'; 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## 常量 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| 名称 | 参数类型 | 可读 | 可写 | 说明 | 28e41f4b71Sopenharmony_ci| ---------------- | -------- | ---- | ---- | --------------------------------------- | 29e41f4b71Sopenharmony_ci| MAX_KEY_LENGTH | number | 是 | 否 | Key的最大长度限制为1024个字节。 | 30e41f4b71Sopenharmony_ci| MAX_VALUE_LENGTH | number | 是 | 否 | Value的最大长度限制为16 * 1024 * 1024个字节。 | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci## preferences.getPreferences 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_cigetPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci获取Preferences实例,使用callback异步回调。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**参数:** 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 46e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 47e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 48e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 49e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误对象。 | 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**错误码:** 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 56e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 57e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 58e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**示例:** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciFA模型示例: 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 65e41f4b71Sopenharmony_ci```ts 66e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 67e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 70e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_cipreferences.getPreferences(context, 'myStore', (err: BusinessError, val: preferences.Preferences) => { 73e41f4b71Sopenharmony_ci if (err) { 74e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 75e41f4b71Sopenharmony_ci return; 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci dataPreferences = val; 78e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 79e41f4b71Sopenharmony_ci}) 80e41f4b71Sopenharmony_ci``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciStage模型示例: 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci```ts 85e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 86e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 87e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 92e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 93e41f4b71Sopenharmony_ci preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: preferences.Preferences) => { 94e41f4b71Sopenharmony_ci if (err) { 95e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 96e41f4b71Sopenharmony_ci return; 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci dataPreferences = val; 99e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 100e41f4b71Sopenharmony_ci }) 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci} 103e41f4b71Sopenharmony_ci``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci## preferences.getPreferences 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_cigetPreferences(context: Context, name: string): Promise<Preferences> 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci获取Preferences实例,使用Promise异步回调。 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**参数:** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 118e41f4b71Sopenharmony_ci| ------- | ------------------------------------- | ---- | ----------------------- | 119e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 120e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**返回值:** 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci| 类型 | 说明 | 125e41f4b71Sopenharmony_ci| ------------------------------------------ | ---------------------------------- | 126e41f4b71Sopenharmony_ci| Promise<[Preferences](#preferences)> | Promise对象,返回Preferences实例。 | 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**错误码:** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 133e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 134e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 135e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**示例:** 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ciFA模型示例: 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 142e41f4b71Sopenharmony_ci```ts 143e41f4b71Sopenharmony_ci// 获取context 144e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 145e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 150e41f4b71Sopenharmony_cilet promise = preferences.getPreferences(context, 'myStore'); 151e41f4b71Sopenharmony_cipromise.then((object: preferences.Preferences) => { 152e41f4b71Sopenharmony_ci dataPreferences = object; 153e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 154e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 155e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 156e41f4b71Sopenharmony_ci}) 157e41f4b71Sopenharmony_ci``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ciStage模型示例: 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci```ts 162e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 163e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 164e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 169e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 170e41f4b71Sopenharmony_ci let promise = preferences.getPreferences(this.context, 'myStore'); 171e41f4b71Sopenharmony_ci promise.then((object: preferences.Preferences) => { 172e41f4b71Sopenharmony_ci dataPreferences = object; 173e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 174e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 175e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 176e41f4b71Sopenharmony_ci }) 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci} 179e41f4b71Sopenharmony_ci``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci## preferences.getPreferences<sup>10+</sup> 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_cigetPreferences(context: Context, options: Options, callback: AsyncCallback<Preferences>): void 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci获取Preferences实例,使用callback异步回调。 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**参数:** 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 194e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 195e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 196e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 197e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误对象。 | 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci**错误码:** 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 204e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 205e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 206e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 207e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 208e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 209e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**示例:** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ciFA模型示例: 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 216e41f4b71Sopenharmony_ci```ts 217e41f4b71Sopenharmony_ci// 获取context 218e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 219e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 222e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 225e41f4b71Sopenharmony_cipreferences.getPreferences(context, options, (err: BusinessError, val: preferences.Preferences) => { 226e41f4b71Sopenharmony_ci if (err) { 227e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 228e41f4b71Sopenharmony_ci return; 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci dataPreferences = val; 231e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 232e41f4b71Sopenharmony_ci}) 233e41f4b71Sopenharmony_ci``` 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ciStage模型示例: 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci```ts 239e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 240e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 241e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 246e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 247e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 248e41f4b71Sopenharmony_ci preferences.getPreferences(this.context, options, (err: BusinessError, val: preferences.Preferences) => { 249e41f4b71Sopenharmony_ci if (err) { 250e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 251e41f4b71Sopenharmony_ci return; 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci dataPreferences = val; 254e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 255e41f4b71Sopenharmony_ci }) 256e41f4b71Sopenharmony_ci } 257e41f4b71Sopenharmony_ci} 258e41f4b71Sopenharmony_ci``` 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci## preferences.getPreferences<sup>10+</sup> 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_cigetPreferences(context: Context, options: Options): Promise<Preferences> 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci获取Preferences实例,使用Promise异步回调。 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci**参数:** 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 273e41f4b71Sopenharmony_ci| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 274e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 275e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci**返回值:** 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci| 类型 | 说明 | 280e41f4b71Sopenharmony_ci| --------------------------------------- | ---------------------------------- | 281e41f4b71Sopenharmony_ci| Promise<[Preferences](#preferences)> | Promise对象,返回Preferences实例。 | 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci**错误码:** 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 288e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 289e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 290e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 291e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 292e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 293e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci**示例:** 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ciFA模型示例: 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 300e41f4b71Sopenharmony_ci```ts 301e41f4b71Sopenharmony_ci// 获取context 302e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 303e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 308e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 309e41f4b71Sopenharmony_cilet promise = preferences.getPreferences(context, options); 310e41f4b71Sopenharmony_cipromise.then((object: preferences.Preferences) => { 311e41f4b71Sopenharmony_ci dataPreferences = object; 312e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 313e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 314e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 315e41f4b71Sopenharmony_ci}) 316e41f4b71Sopenharmony_ci``` 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ciStage模型示例: 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci```ts 321e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 322e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 323e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 328e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 329e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 330e41f4b71Sopenharmony_ci let promise = preferences.getPreferences(this.context, options); 331e41f4b71Sopenharmony_ci promise.then((object: preferences.Preferences) => { 332e41f4b71Sopenharmony_ci dataPreferences = object; 333e41f4b71Sopenharmony_ci console.info("Succeeded in getting preferences."); 334e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 335e41f4b71Sopenharmony_ci console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); 336e41f4b71Sopenharmony_ci }) 337e41f4b71Sopenharmony_ci } 338e41f4b71Sopenharmony_ci} 339e41f4b71Sopenharmony_ci``` 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci## preferences.getPreferencesSync<sup>10+</sup> 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_cigetPreferencesSync(context: Context, options: Options): Preferences 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci获取Preferences实例,此为同步接口。 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**参数:** 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 354e41f4b71Sopenharmony_ci| ------- | --------------------- | ---- | ------------------------------------------------------------ | 355e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 356e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci**返回值:** 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci| 类型 | 说明 | 361e41f4b71Sopenharmony_ci| --------------------------- | --------------------- | 362e41f4b71Sopenharmony_ci| [Preferences](#preferences) | 返回Preferences实例。 | 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci**错误码:** 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 369e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 370e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 371e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 372e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 373e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 374e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**示例:** 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ciFA模型示例: 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 381e41f4b71Sopenharmony_ci```ts 382e41f4b71Sopenharmony_ci// 获取context 383e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 386e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 389e41f4b71Sopenharmony_cidataPreferences = preferences.getPreferencesSync(context, options); 390e41f4b71Sopenharmony_ci``` 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ciStage模型示例: 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci```ts 395e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 396e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_cilet dataPreferences: preferences.Preferences | null = null; 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 401e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 402e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 403e41f4b71Sopenharmony_ci dataPreferences = preferences.getPreferencesSync(this.context, options); 404e41f4b71Sopenharmony_ci } 405e41f4b71Sopenharmony_ci} 406e41f4b71Sopenharmony_ci``` 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci## preferences.deletePreferences 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_cideletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,若Preferences实例有对应的持久化文件,则同时删除其持久化文件。使用callback异步回调。 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci不支持该接口与preference其他接口并发调用。 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci**参数:** 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 425e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | 426e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 427e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 428e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 | 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**错误码:** 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 435e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 436e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 437e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 438e41f4b71Sopenharmony_ci| 15500010 | Failed to delete preferences file. | 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci**示例:** 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ciFA模型示例: 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 445e41f4b71Sopenharmony_ci```ts 446e41f4b71Sopenharmony_ci// 获取context 447e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 448e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_cipreferences.deletePreferences(context, 'myStore', (err: BusinessError) => { 453e41f4b71Sopenharmony_ci if (err) { 454e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 455e41f4b71Sopenharmony_ci return; 456e41f4b71Sopenharmony_ci } 457e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 458e41f4b71Sopenharmony_ci}) 459e41f4b71Sopenharmony_ci``` 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ciStage模型示例: 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci```ts 464e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 465e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 466e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 469e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 470e41f4b71Sopenharmony_ci preferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => { 471e41f4b71Sopenharmony_ci if (err) { 472e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 473e41f4b71Sopenharmony_ci return; 474e41f4b71Sopenharmony_ci } 475e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 476e41f4b71Sopenharmony_ci }) 477e41f4b71Sopenharmony_ci } 478e41f4b71Sopenharmony_ci} 479e41f4b71Sopenharmony_ci``` 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci## preferences.deletePreferences 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_cideletePreferences(context: Context, name: string): Promise<void> 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,若Preferences实例有对应的持久化文件,则同时删除其持久化文件。使用Promise异步回调。 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci不支持该接口与preference其他接口并发调用。 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci**参数:** 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 498e41f4b71Sopenharmony_ci| ------- | ------------------------------------- | ---- | ----------------------- | 499e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 500e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci**返回值:** 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci| 类型 | 说明 | 505e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 506e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci**错误码:** 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 513e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 514e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 515e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 516e41f4b71Sopenharmony_ci| 15500010 | Failed to delete preferences file. | 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci**示例:** 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ciFA模型示例: 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 523e41f4b71Sopenharmony_ci```ts 524e41f4b71Sopenharmony_ci// 获取context 525e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 526e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_cilet promise = preferences.deletePreferences(context, 'myStore'); 531e41f4b71Sopenharmony_cipromise.then(() => { 532e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 533e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 534e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 535e41f4b71Sopenharmony_ci}) 536e41f4b71Sopenharmony_ci``` 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ciStage模型示例: 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci```ts 541e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 542e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 543e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 546e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 547e41f4b71Sopenharmony_ci let promise = preferences.deletePreferences(this.context, 'myStore'); 548e41f4b71Sopenharmony_ci promise.then(() => { 549e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 550e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 551e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 552e41f4b71Sopenharmony_ci }) 553e41f4b71Sopenharmony_ci } 554e41f4b71Sopenharmony_ci} 555e41f4b71Sopenharmony_ci``` 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ci## preferences.deletePreferences<sup>10+</sup> 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_cideletePreferences(context: Context, options: Options, callback: AsyncCallback<void>): void 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,若Preferences实例有对应的持久化文件,则同时删除其持久化文件。使用callback异步回调。 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 564e41f4b71Sopenharmony_ci 565e41f4b71Sopenharmony_ci不支持该接口与preference其他接口并发调用。 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ci**参数:** 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 574e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 575e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 576e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 577e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 | 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci**错误码:** 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 584e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 585e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 586e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 587e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 588e41f4b71Sopenharmony_ci| 15500010 | Failed to delete preferences file. | 589e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 590e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci**示例:** 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ciFA模型示例: 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 597e41f4b71Sopenharmony_ci```ts 598e41f4b71Sopenharmony_ci// 获取context 599e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 600e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 605e41f4b71Sopenharmony_cipreferences.deletePreferences(context, options, (err: BusinessError) => { 606e41f4b71Sopenharmony_ci if (err) { 607e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 608e41f4b71Sopenharmony_ci return; 609e41f4b71Sopenharmony_ci } 610e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 611e41f4b71Sopenharmony_ci}) 612e41f4b71Sopenharmony_ci``` 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ciStage模型示例: 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci```ts 617e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 618e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 619e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 622e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 623e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 624e41f4b71Sopenharmony_ci preferences.deletePreferences(this.context, options, (err: BusinessError) => { 625e41f4b71Sopenharmony_ci if (err) { 626e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 627e41f4b71Sopenharmony_ci return; 628e41f4b71Sopenharmony_ci } 629e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 630e41f4b71Sopenharmony_ci }) 631e41f4b71Sopenharmony_ci } 632e41f4b71Sopenharmony_ci} 633e41f4b71Sopenharmony_ci``` 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ci## preferences.deletePreferences<sup>10+</sup> 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_cideletePreferences(context: Context, options: Options): Promise<void> 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,若Preferences实例有对应的持久化文件,则同时删除其持久化文件。使用Promise异步回调。 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci不支持该接口与preference其他接口并发调用。 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 647e41f4b71Sopenharmony_ci 648e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci**参数:** 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 653e41f4b71Sopenharmony_ci| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 654e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 655e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci**返回值:** 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci| 类型 | 说明 | 660e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 661e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**错误码:** 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 668e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 669e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 670e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 671e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 672e41f4b71Sopenharmony_ci| 15500010 | Failed to delete preferences file. | 673e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 674e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 675e41f4b71Sopenharmony_ci 676e41f4b71Sopenharmony_ci**示例:** 677e41f4b71Sopenharmony_ci 678e41f4b71Sopenharmony_ciFA模型示例: 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 681e41f4b71Sopenharmony_ci```ts 682e41f4b71Sopenharmony_ci// 获取context 683e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 684e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 685e41f4b71Sopenharmony_ci 686e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 689e41f4b71Sopenharmony_cilet promise = preferences.deletePreferences(context, options); 690e41f4b71Sopenharmony_cipromise.then(() => { 691e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 692e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 693e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 694e41f4b71Sopenharmony_ci}) 695e41f4b71Sopenharmony_ci``` 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ciStage模型示例: 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci```ts 700e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 701e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 702e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 703e41f4b71Sopenharmony_ci 704e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 705e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 706e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 707e41f4b71Sopenharmony_ci let promise = preferences.deletePreferences(this.context, options); 708e41f4b71Sopenharmony_ci promise.then(() => { 709e41f4b71Sopenharmony_ci console.info("Succeeded in deleting preferences."); 710e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 711e41f4b71Sopenharmony_ci console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); 712e41f4b71Sopenharmony_ci }) 713e41f4b71Sopenharmony_ci } 714e41f4b71Sopenharmony_ci} 715e41f4b71Sopenharmony_ci``` 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci 718e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCache 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ciremovePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,使用callback异步回调。 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci**参数:** 733e41f4b71Sopenharmony_ci 734e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 735e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | 736e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 737e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 738e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 | 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci**错误码:** 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 745e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 746e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 747e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ci**示例:** 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ciFA模型示例: 752e41f4b71Sopenharmony_ci 753e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 754e41f4b71Sopenharmony_ci```ts 755e41f4b71Sopenharmony_ci// 获取context 756e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 757e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 760e41f4b71Sopenharmony_cipreferences.removePreferencesFromCache(context, 'myStore', (err: BusinessError) => { 761e41f4b71Sopenharmony_ci if (err) { 762e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 763e41f4b71Sopenharmony_ci return; 764e41f4b71Sopenharmony_ci } 765e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 766e41f4b71Sopenharmony_ci}) 767e41f4b71Sopenharmony_ci``` 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ciStage模型示例: 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci```ts 772e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 773e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 774e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 775e41f4b71Sopenharmony_ci 776e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 777e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 778e41f4b71Sopenharmony_ci preferences.removePreferencesFromCache(this.context, 'myStore', (err: BusinessError) => { 779e41f4b71Sopenharmony_ci if (err) { 780e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 781e41f4b71Sopenharmony_ci return; 782e41f4b71Sopenharmony_ci } 783e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 784e41f4b71Sopenharmony_ci }) 785e41f4b71Sopenharmony_ci } 786e41f4b71Sopenharmony_ci} 787e41f4b71Sopenharmony_ci``` 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCache 790e41f4b71Sopenharmony_ci 791e41f4b71Sopenharmony_ciremovePreferencesFromCache(context: Context, name: string): Promise<void> 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,使用Promise异步回调。 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ci**参数:** 804e41f4b71Sopenharmony_ci 805e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 806e41f4b71Sopenharmony_ci| ------- | ------------------------------------- | ---- | ----------------------- | 807e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 808e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci**返回值:** 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci| 类型 | 说明 | 813e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 814e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci**错误码:** 817e41f4b71Sopenharmony_ci 818e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 821e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 822e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 823e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci**示例:** 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ciFA模型示例: 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 830e41f4b71Sopenharmony_ci```ts 831e41f4b71Sopenharmony_ci// 获取context 832e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 833e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 834e41f4b71Sopenharmony_ci 835e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 836e41f4b71Sopenharmony_cilet promise = preferences.removePreferencesFromCache(context, 'myStore'); 837e41f4b71Sopenharmony_cipromise.then(() => { 838e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 839e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 840e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 841e41f4b71Sopenharmony_ci}) 842e41f4b71Sopenharmony_ci``` 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ciStage模型示例: 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci```ts 847e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 848e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 849e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 850e41f4b71Sopenharmony_ci 851e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 852e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 853e41f4b71Sopenharmony_ci let promise = preferences.removePreferencesFromCache(this.context, 'myStore'); 854e41f4b71Sopenharmony_ci promise.then(() => { 855e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 856e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 857e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 858e41f4b71Sopenharmony_ci }) 859e41f4b71Sopenharmony_ci } 860e41f4b71Sopenharmony_ci} 861e41f4b71Sopenharmony_ci``` 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCacheSync<sup>10+</sup> 864e41f4b71Sopenharmony_ci 865e41f4b71Sopenharmony_ciremovePreferencesFromCacheSync(context: Context, name: string): void 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,此为同步接口。 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci**参数:** 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 880e41f4b71Sopenharmony_ci| ------- | ------------------------------------- | ---- | ----------------------- | 881e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 882e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 883e41f4b71Sopenharmony_ci 884e41f4b71Sopenharmony_ci**错误码:** 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 889e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 890e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 891e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 892e41f4b71Sopenharmony_ci 893e41f4b71Sopenharmony_ci**示例:** 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ciFA模型示例: 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 898e41f4b71Sopenharmony_ci```ts 899e41f4b71Sopenharmony_ci// 获取context 900e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 901e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 902e41f4b71Sopenharmony_cipreferences.removePreferencesFromCacheSync(context, 'myStore'); 903e41f4b71Sopenharmony_ci``` 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ciStage模型示例: 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ci```ts 908e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 909e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 912e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 913e41f4b71Sopenharmony_ci preferences.removePreferencesFromCacheSync(this.context, 'myStore'); 914e41f4b71Sopenharmony_ci } 915e41f4b71Sopenharmony_ci} 916e41f4b71Sopenharmony_ci``` 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCache<sup>10+</sup> 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ciremovePreferencesFromCache(context: Context, options: Options, callback: AsyncCallback<void>): void 921e41f4b71Sopenharmony_ci 922e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,使用callback异步回调。 923e41f4b71Sopenharmony_ci 924e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 927e41f4b71Sopenharmony_ci 928e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 929e41f4b71Sopenharmony_ci 930e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ci**参数:** 933e41f4b71Sopenharmony_ci 934e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 935e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 936e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 937e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 938e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误对象。 | 939e41f4b71Sopenharmony_ci 940e41f4b71Sopenharmony_ci**错误码:** 941e41f4b71Sopenharmony_ci 942e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 943e41f4b71Sopenharmony_ci 944e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 945e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 946e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 947e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 948e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 949e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 950e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 951e41f4b71Sopenharmony_ci 952e41f4b71Sopenharmony_ci**示例:** 953e41f4b71Sopenharmony_ci 954e41f4b71Sopenharmony_ciFA模型示例: 955e41f4b71Sopenharmony_ci 956e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 957e41f4b71Sopenharmony_ci```ts 958e41f4b71Sopenharmony_ci// 获取context 959e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 960e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 961e41f4b71Sopenharmony_ci 962e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 963e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 964e41f4b71Sopenharmony_cipreferences.removePreferencesFromCache(context, options, (err: BusinessError) => { 965e41f4b71Sopenharmony_ci if (err) { 966e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 967e41f4b71Sopenharmony_ci return; 968e41f4b71Sopenharmony_ci } 969e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 970e41f4b71Sopenharmony_ci}) 971e41f4b71Sopenharmony_ci``` 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ciStage模型示例: 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ci```ts 976e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 977e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 978e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 979e41f4b71Sopenharmony_ci 980e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 981e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 982e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 983e41f4b71Sopenharmony_ci preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => { 984e41f4b71Sopenharmony_ci if (err) { 985e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 986e41f4b71Sopenharmony_ci return; 987e41f4b71Sopenharmony_ci } 988e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 989e41f4b71Sopenharmony_ci }) 990e41f4b71Sopenharmony_ci } 991e41f4b71Sopenharmony_ci} 992e41f4b71Sopenharmony_ci``` 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCache<sup>10+</sup> 995e41f4b71Sopenharmony_ci 996e41f4b71Sopenharmony_ciremovePreferencesFromCache(context: Context, options: Options): Promise<void> 997e41f4b71Sopenharmony_ci 998e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,使用Promise异步回调。 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 1001e41f4b71Sopenharmony_ci 1002e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 1003e41f4b71Sopenharmony_ci 1004e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1005e41f4b71Sopenharmony_ci 1006e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1007e41f4b71Sopenharmony_ci 1008e41f4b71Sopenharmony_ci**参数:** 1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1011e41f4b71Sopenharmony_ci| ------- | ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 1012e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 1013e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci**返回值:** 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci| 类型 | 说明 | 1018e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 1019e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ci**错误码:** 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1026e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1027e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1028e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 1029e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1030e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 1031e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 1032e41f4b71Sopenharmony_ci 1033e41f4b71Sopenharmony_ci**示例:** 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ciFA模型示例: 1036e41f4b71Sopenharmony_ci 1037e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 1038e41f4b71Sopenharmony_ci```ts 1039e41f4b71Sopenharmony_ci// 获取context 1040e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 1041e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1042e41f4b71Sopenharmony_ci 1043e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 1044e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 1045e41f4b71Sopenharmony_cilet promise = preferences.removePreferencesFromCache(context, options); 1046e41f4b71Sopenharmony_cipromise.then(() => { 1047e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 1048e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1049e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 1050e41f4b71Sopenharmony_ci}) 1051e41f4b71Sopenharmony_ci``` 1052e41f4b71Sopenharmony_ci 1053e41f4b71Sopenharmony_ciStage模型示例: 1054e41f4b71Sopenharmony_ci 1055e41f4b71Sopenharmony_ci```ts 1056e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 1057e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1058e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 1061e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 1062e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 1063e41f4b71Sopenharmony_ci let promise = preferences.removePreferencesFromCache(this.context, options); 1064e41f4b71Sopenharmony_ci promise.then(() => { 1065e41f4b71Sopenharmony_ci console.info("Succeeded in removing preferences."); 1066e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1067e41f4b71Sopenharmony_ci console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); 1068e41f4b71Sopenharmony_ci }) 1069e41f4b71Sopenharmony_ci } 1070e41f4b71Sopenharmony_ci} 1071e41f4b71Sopenharmony_ci``` 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci## preferences.removePreferencesFromCacheSync<sup>10+</sup> 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ciremovePreferencesFromCacheSync(context: Context, options: Options):void 1076e41f4b71Sopenharmony_ci 1077e41f4b71Sopenharmony_ci从缓存中移出指定的Preferences实例,此为同步接口。 1078e41f4b71Sopenharmony_ci 1079e41f4b71Sopenharmony_ci应用首次调用[getPreferences](#preferencesgetpreferences)接口获取某个Preferences实例后,该实例会被会被缓存起来,后续再次[getPreferences](#preferencesgetpreferences)时不会再次从持久化文件中读取,直接从缓存中获取Preferences实例。调用此接口移出缓存中的实例之后,再次getPreferences将会重新读取持久化文件,生成新的Preferences实例。 1080e41f4b71Sopenharmony_ci 1081e41f4b71Sopenharmony_ci调用该接口后,不建议再使用旧的Preferences实例进行数据操作,否则会出现数据一致性问题,应将Preferences实例置为null,系统将会统一回收。 1082e41f4b71Sopenharmony_ci 1083e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1084e41f4b71Sopenharmony_ci 1085e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1086e41f4b71Sopenharmony_ci 1087e41f4b71Sopenharmony_ci**参数:** 1088e41f4b71Sopenharmony_ci 1089e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1090e41f4b71Sopenharmony_ci| ------- | --------------------- | ---- | ------------------------------------------------------------ | 1091e41f4b71Sopenharmony_ci| context | Context | 是 | 应用上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 1092e41f4b71Sopenharmony_ci| options | [Options](#options10) | 是 | 与Preferences实例相关的配置选项。 | 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci**错误码:** 1095e41f4b71Sopenharmony_ci 1096e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1097e41f4b71Sopenharmony_ci 1098e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1099e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1100e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1101e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 1102e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1103e41f4b71Sopenharmony_ci| 15501001 | Only supported in stage mode. | 1104e41f4b71Sopenharmony_ci| 15501002 | The data group id is not valid. | 1105e41f4b71Sopenharmony_ci 1106e41f4b71Sopenharmony_ci**示例:** 1107e41f4b71Sopenharmony_ci 1108e41f4b71Sopenharmony_ciFA模型示例: 1109e41f4b71Sopenharmony_ci 1110e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 1111e41f4b71Sopenharmony_ci```ts 1112e41f4b71Sopenharmony_ci// 获取context 1113e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 1114e41f4b71Sopenharmony_cilet context = featureAbility.getContext(); 1115e41f4b71Sopenharmony_cilet options: preferences.Options = { name: 'myStore' }; 1116e41f4b71Sopenharmony_cipreferences.removePreferencesFromCacheSync(context, options); 1117e41f4b71Sopenharmony_ci``` 1118e41f4b71Sopenharmony_ci 1119e41f4b71Sopenharmony_ciStage模型示例: 1120e41f4b71Sopenharmony_ci 1121e41f4b71Sopenharmony_ci```ts 1122e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 1123e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 1124e41f4b71Sopenharmony_ci 1125e41f4b71Sopenharmony_ciclass EntryAbility extends UIAbility { 1126e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 1127e41f4b71Sopenharmony_ci let options: preferences.Options = { name: 'myStore' }; 1128e41f4b71Sopenharmony_ci preferences.removePreferencesFromCacheSync(this.context, options); 1129e41f4b71Sopenharmony_ci } 1130e41f4b71Sopenharmony_ci} 1131e41f4b71Sopenharmony_ci``` 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_ci## Options<sup>10+</sup> 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ciPreferences实例配置选项。 1136e41f4b71Sopenharmony_ci 1137e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1138e41f4b71Sopenharmony_ci 1139e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 1142e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ------------------------------------------------------------ | 1143e41f4b71Sopenharmony_ci| name | string | 是 | Preferences实例的名称。 | 1144e41f4b71Sopenharmony_ci| dataGroupId | string\|null\|undefined | 否 | 应用组ID,需要向应用市场获取,暂不支持。<br/>为可选参数。指定在此dataGroupId对应的沙箱路径下创建Preferences实例。当此参数不填时,默认在本应用沙箱目录下创建Preferences实例。<br/> **模型约束:** 此属性仅在Stage模型下可用。| 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ci 1147e41f4b71Sopenharmony_ci## Preferences 1148e41f4b71Sopenharmony_ci 1149e41f4b71Sopenharmony_ci首选项实例,提供获取和修改存储数据的接口。 1150e41f4b71Sopenharmony_ci 1151e41f4b71Sopenharmony_ci下列接口都需先使用[preferences.getPreferences](#preferencesgetpreferences)获取到Preferences实例,再通过此实例调用对应接口。 1152e41f4b71Sopenharmony_ci 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci### get 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ciget(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void 1157e41f4b71Sopenharmony_ci 1158e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue,使用callback异步回调。 1159e41f4b71Sopenharmony_ci 1160e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1163e41f4b71Sopenharmony_ci 1164e41f4b71Sopenharmony_ci**参数:** 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1167e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- |---------------------------| 1168e41f4b71Sopenharmony_ci| key | string | 是 | 要获取的存储Key名称,不能为空。 | 1169e41f4b71Sopenharmony_ci| defValue | [ValueType](#valuetype) | 是 | 默认返回值。 | 1170e41f4b71Sopenharmony_ci| callback | AsyncCallback<[ValueType](#valuetype)> | 是 | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误对象。 | 1171e41f4b71Sopenharmony_ci 1172e41f4b71Sopenharmony_ci**错误码:** 1173e41f4b71Sopenharmony_ci 1174e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1175e41f4b71Sopenharmony_ci 1176e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1177e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1178e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1179e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci**示例:** 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ci```ts 1184e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_cidataPreferences.get('startup', 'default', (err: BusinessError, val: preferences.ValueType) => { 1187e41f4b71Sopenharmony_ci if (err) { 1188e41f4b71Sopenharmony_ci console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); 1189e41f4b71Sopenharmony_ci return; 1190e41f4b71Sopenharmony_ci } 1191e41f4b71Sopenharmony_ci console.info("Succeeded in getting value of 'startup'. val: " + val); 1192e41f4b71Sopenharmony_ci}) 1193e41f4b71Sopenharmony_ci``` 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci### get 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ciget(key: string, defValue: ValueType): Promise<ValueType> 1198e41f4b71Sopenharmony_ci 1199e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue,使用Promise异步回调。 1200e41f4b71Sopenharmony_ci 1201e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1202e41f4b71Sopenharmony_ci 1203e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci **参数:** 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1208e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- |--------| 1209e41f4b71Sopenharmony_ci| key | string | 是 | 要获取的存储Key名称,不能为空。 | 1210e41f4b71Sopenharmony_ci| defValue | [ValueType](#valuetype) | 是 | 默认返回值。 | 1211e41f4b71Sopenharmony_ci 1212e41f4b71Sopenharmony_ci**返回值:** 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_ci| 类型 | 说明 | 1215e41f4b71Sopenharmony_ci| ----------------------------------- | ----------------------------- | 1216e41f4b71Sopenharmony_ci| Promise<[ValueType](#valuetype)> | Promise对象,返回键对应的值。 | 1217e41f4b71Sopenharmony_ci 1218e41f4b71Sopenharmony_ci**错误码:** 1219e41f4b71Sopenharmony_ci 1220e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1221e41f4b71Sopenharmony_ci 1222e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1223e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1224e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1225e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_ci**示例:** 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ci```ts 1230e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_cilet promise = dataPreferences.get('startup', 'default'); 1233e41f4b71Sopenharmony_cipromise.then((data: preferences.ValueType) => { 1234e41f4b71Sopenharmony_ci console.info("Succeeded in getting value of 'startup'. Data: " + data); 1235e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1236e41f4b71Sopenharmony_ci console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); 1237e41f4b71Sopenharmony_ci}) 1238e41f4b71Sopenharmony_ci``` 1239e41f4b71Sopenharmony_ci 1240e41f4b71Sopenharmony_ci### getSync<sup>10+</sup> 1241e41f4b71Sopenharmony_ci 1242e41f4b71Sopenharmony_cigetSync(key: string, defValue: ValueType): ValueType 1243e41f4b71Sopenharmony_ci 1244e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取键对应的值,如果值为null或者非默认值类型,返回默认数据defValue,此为同步接口。 1245e41f4b71Sopenharmony_ci 1246e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1247e41f4b71Sopenharmony_ci 1248e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1249e41f4b71Sopenharmony_ci 1250e41f4b71Sopenharmony_ci**参数:** 1251e41f4b71Sopenharmony_ci 1252e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1253e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- |---------------------| 1254e41f4b71Sopenharmony_ci| key | string | 是 | 要获取的存储Key名称,不能为空。 | 1255e41f4b71Sopenharmony_ci| defValue | [ValueType](#valuetype) | 是 | 默认返回值。 | 1256e41f4b71Sopenharmony_ci 1257e41f4b71Sopenharmony_ci**返回值:** 1258e41f4b71Sopenharmony_ci 1259e41f4b71Sopenharmony_ci| 类型 | 说明 | 1260e41f4b71Sopenharmony_ci| ----------------------------------- | ----------------------------- | 1261e41f4b71Sopenharmony_ci| [ValueType](#valuetype) | 返回键对应的值。 | 1262e41f4b71Sopenharmony_ci 1263e41f4b71Sopenharmony_ci**错误码:** 1264e41f4b71Sopenharmony_ci 1265e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1268e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1269e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1270e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci**示例:** 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_ci```ts 1275e41f4b71Sopenharmony_cilet value: preferences.ValueType = dataPreferences.getSync('startup', 'default'); 1276e41f4b71Sopenharmony_ci``` 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci### getAll 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_cigetAll(callback: AsyncCallback<Object>): void; 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取所有键值数据。 1283e41f4b71Sopenharmony_ci 1284e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1285e41f4b71Sopenharmony_ci 1286e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1287e41f4b71Sopenharmony_ci 1288e41f4b71Sopenharmony_ci**参数:** 1289e41f4b71Sopenharmony_ci 1290e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1291e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 1292e41f4b71Sopenharmony_ci| callback | AsyncCallback<Object> | 是 | 回调函数。当获取成功,err为undefined,value为所有键值数据;否则err为错误对象。 | 1293e41f4b71Sopenharmony_ci 1294e41f4b71Sopenharmony_ci**错误码:** 1295e41f4b71Sopenharmony_ci 1296e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1297e41f4b71Sopenharmony_ci 1298e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1299e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1300e41f4b71Sopenharmony_ci| 401 | Parameter error. Mandatory parameters are left unspecified.| 1301e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci**示例:** 1304e41f4b71Sopenharmony_ci 1305e41f4b71Sopenharmony_ci```ts 1306e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1307e41f4b71Sopenharmony_ci 1308e41f4b71Sopenharmony_ci// 由于ArkTS中无Object.keys,且无法使用for..in... 1309e41f4b71Sopenharmony_ci// 若报ArkTS问题,请将此方法单独抽离至一个ts文件中并暴露,在需要用到的ets文件中引入使用 1310e41f4b71Sopenharmony_cifunction getObjKeys(obj: Object): string[] { 1311e41f4b71Sopenharmony_ci let keys = Object.keys(obj); 1312e41f4b71Sopenharmony_ci return keys; 1313e41f4b71Sopenharmony_ci} 1314e41f4b71Sopenharmony_ci 1315e41f4b71Sopenharmony_cidataPreferences.getAll((err: BusinessError, value: Object) => { 1316e41f4b71Sopenharmony_ci if (err) { 1317e41f4b71Sopenharmony_ci console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); 1318e41f4b71Sopenharmony_ci return; 1319e41f4b71Sopenharmony_ci } 1320e41f4b71Sopenharmony_ci let allKeys = getObjKeys(value); 1321e41f4b71Sopenharmony_ci console.info("getAll keys = " + allKeys); 1322e41f4b71Sopenharmony_ci console.info("getAll object = " + JSON.stringify(value)); 1323e41f4b71Sopenharmony_ci}) 1324e41f4b71Sopenharmony_ci``` 1325e41f4b71Sopenharmony_ci 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_ci### getAll 1328e41f4b71Sopenharmony_ci 1329e41f4b71Sopenharmony_cigetAll(): Promise<Object> 1330e41f4b71Sopenharmony_ci 1331e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取所有键值数据。 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1334e41f4b71Sopenharmony_ci 1335e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1336e41f4b71Sopenharmony_ci 1337e41f4b71Sopenharmony_ci**返回值:** 1338e41f4b71Sopenharmony_ci 1339e41f4b71Sopenharmony_ci| 类型 | 说明 | 1340e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------- | 1341e41f4b71Sopenharmony_ci| Promise<Object> | Promise对象,返回含有所有键值数据。 | 1342e41f4b71Sopenharmony_ci 1343e41f4b71Sopenharmony_ci**错误码:** 1344e41f4b71Sopenharmony_ci 1345e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[用户首选项错误码](errorcode-preferences.md)。 1346e41f4b71Sopenharmony_ci 1347e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1348e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1349e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1350e41f4b71Sopenharmony_ci 1351e41f4b71Sopenharmony_ci**示例:** 1352e41f4b71Sopenharmony_ci 1353e41f4b71Sopenharmony_ci```ts 1354e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1355e41f4b71Sopenharmony_ci 1356e41f4b71Sopenharmony_ci// 由于ArkTS中无Object.keys,且无法使用for..in... 1357e41f4b71Sopenharmony_ci// 若报ArkTS问题,请将此方法单独抽离至一个ts文件中并暴露,在需要用到的ets文件中引入使用 1358e41f4b71Sopenharmony_cifunction getObjKeys(obj: Object): string[] { 1359e41f4b71Sopenharmony_ci let keys = Object.keys(obj); 1360e41f4b71Sopenharmony_ci return keys; 1361e41f4b71Sopenharmony_ci} 1362e41f4b71Sopenharmony_ci 1363e41f4b71Sopenharmony_cilet promise = dataPreferences.getAll(); 1364e41f4b71Sopenharmony_cipromise.then((value: Object) => { 1365e41f4b71Sopenharmony_ci let allKeys = getObjKeys(value); 1366e41f4b71Sopenharmony_ci console.info('getAll keys = ' + allKeys); 1367e41f4b71Sopenharmony_ci console.info("getAll object = " + JSON.stringify(value)); 1368e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1369e41f4b71Sopenharmony_ci console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); 1370e41f4b71Sopenharmony_ci}) 1371e41f4b71Sopenharmony_ci``` 1372e41f4b71Sopenharmony_ci 1373e41f4b71Sopenharmony_ci### getAllSync<sup>10+</sup> 1374e41f4b71Sopenharmony_ci 1375e41f4b71Sopenharmony_cigetAllSync(): Object 1376e41f4b71Sopenharmony_ci 1377e41f4b71Sopenharmony_ci从缓存的Preferences实例中获取所有键值数据,此为同步接口。 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1380e41f4b71Sopenharmony_ci 1381e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci**返回值:** 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci| 类型 | 说明 | 1386e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------- | 1387e41f4b71Sopenharmony_ci| Object | 返回含有所有键值数据。 | 1388e41f4b71Sopenharmony_ci 1389e41f4b71Sopenharmony_ci**错误码:** 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[用户首选项错误码](errorcode-preferences.md)。 1392e41f4b71Sopenharmony_ci 1393e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1394e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1395e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1396e41f4b71Sopenharmony_ci 1397e41f4b71Sopenharmony_ci**示例:** 1398e41f4b71Sopenharmony_ci 1399e41f4b71Sopenharmony_ci```ts 1400e41f4b71Sopenharmony_ci// 由于ArkTS中无Object.keys,且无法使用for..in... 1401e41f4b71Sopenharmony_ci// 若报ArkTS问题,请将此方法单独抽离至一个ts文件中并暴露,在需要用到的ets文件中引入使用 1402e41f4b71Sopenharmony_cifunction getObjKeys(obj: Object): string[] { 1403e41f4b71Sopenharmony_ci let keys = Object.keys(obj); 1404e41f4b71Sopenharmony_ci return keys; 1405e41f4b71Sopenharmony_ci} 1406e41f4b71Sopenharmony_ci 1407e41f4b71Sopenharmony_cilet value = dataPreferences.getAllSync(); 1408e41f4b71Sopenharmony_cilet allKeys = getObjKeys(value); 1409e41f4b71Sopenharmony_ciconsole.info('getAll keys = ' + allKeys); 1410e41f4b71Sopenharmony_ciconsole.info("getAll object = " + JSON.stringify(value)); 1411e41f4b71Sopenharmony_ci``` 1412e41f4b71Sopenharmony_ci 1413e41f4b71Sopenharmony_ci### put 1414e41f4b71Sopenharmony_ci 1415e41f4b71Sopenharmony_ciput(key: string, value: ValueType, callback: AsyncCallback<void>): void 1416e41f4b71Sopenharmony_ci 1417e41f4b71Sopenharmony_ci将数据写入缓存的Preferences实例中,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。 1418e41f4b71Sopenharmony_ci 1419e41f4b71Sopenharmony_ci > **说明:** 1420e41f4b71Sopenharmony_ci > 1421e41f4b71Sopenharmony_ci > 当对应的键已经存在时,put()方法会覆盖其值。可以使用hasSync()方法检查是否存在对应键值对。 1422e41f4b71Sopenharmony_ci 1423e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1424e41f4b71Sopenharmony_ci 1425e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1426e41f4b71Sopenharmony_ci 1427e41f4b71Sopenharmony_ci**参数:** 1428e41f4b71Sopenharmony_ci 1429e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1430e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- |-------------------------| 1431e41f4b71Sopenharmony_ci| key | string | 是 | 要修改的存储的Key,不能为空。 | 1432e41f4b71Sopenharmony_ci| value | [ValueType](#valuetype) | 是 | 存储的新值。 | 1433e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当数据写入成功,err为undefined;否则为错误对象。 | 1434e41f4b71Sopenharmony_ci 1435e41f4b71Sopenharmony_ci**错误码:** 1436e41f4b71Sopenharmony_ci 1437e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1438e41f4b71Sopenharmony_ci 1439e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1440e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1441e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1442e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1443e41f4b71Sopenharmony_ci 1444e41f4b71Sopenharmony_ci**示例:** 1445e41f4b71Sopenharmony_ci 1446e41f4b71Sopenharmony_ci```ts 1447e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1448e41f4b71Sopenharmony_ci 1449e41f4b71Sopenharmony_cidataPreferences.put('startup', 'auto', (err: BusinessError) => { 1450e41f4b71Sopenharmony_ci if (err) { 1451e41f4b71Sopenharmony_ci console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); 1452e41f4b71Sopenharmony_ci return; 1453e41f4b71Sopenharmony_ci } 1454e41f4b71Sopenharmony_ci console.info("Succeeded in putting value of 'startup'."); 1455e41f4b71Sopenharmony_ci}) 1456e41f4b71Sopenharmony_ci``` 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ci 1459e41f4b71Sopenharmony_ci### put 1460e41f4b71Sopenharmony_ci 1461e41f4b71Sopenharmony_ciput(key: string, value: ValueType): Promise<void> 1462e41f4b71Sopenharmony_ci 1463e41f4b71Sopenharmony_ci将数据写入缓存的Preferences实例中,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。 1464e41f4b71Sopenharmony_ci 1465e41f4b71Sopenharmony_ci > **说明:** 1466e41f4b71Sopenharmony_ci > 1467e41f4b71Sopenharmony_ci > 当对应的键已经存在时,put()方法会覆盖其值。可以使用hasSync()方法检查是否存在对应键值对。 1468e41f4b71Sopenharmony_ci 1469e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1470e41f4b71Sopenharmony_ci 1471e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci**参数:** 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1476e41f4b71Sopenharmony_ci| ------ | ----------------------- | ---- |--------------------------| 1477e41f4b71Sopenharmony_ci| key | string | 是 | 要修改的存储的Key,不能为空。 | 1478e41f4b71Sopenharmony_ci| value | [ValueType](#valuetype) | 是 | 存储的新值。 | 1479e41f4b71Sopenharmony_ci 1480e41f4b71Sopenharmony_ci**返回值:** 1481e41f4b71Sopenharmony_ci 1482e41f4b71Sopenharmony_ci| 类型 | 说明 | 1483e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 1484e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 1485e41f4b71Sopenharmony_ci 1486e41f4b71Sopenharmony_ci**错误码:** 1487e41f4b71Sopenharmony_ci 1488e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1489e41f4b71Sopenharmony_ci 1490e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1491e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1492e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1493e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1494e41f4b71Sopenharmony_ci 1495e41f4b71Sopenharmony_ci**示例:** 1496e41f4b71Sopenharmony_ci 1497e41f4b71Sopenharmony_ci```ts 1498e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1499e41f4b71Sopenharmony_ci 1500e41f4b71Sopenharmony_cilet promise = dataPreferences.put('startup', 'auto'); 1501e41f4b71Sopenharmony_cipromise.then(() => { 1502e41f4b71Sopenharmony_ci console.info("Succeeded in putting value of 'startup'."); 1503e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1504e41f4b71Sopenharmony_ci console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); 1505e41f4b71Sopenharmony_ci}) 1506e41f4b71Sopenharmony_ci``` 1507e41f4b71Sopenharmony_ci 1508e41f4b71Sopenharmony_ci 1509e41f4b71Sopenharmony_ci### putSync<sup>10+</sup> 1510e41f4b71Sopenharmony_ci 1511e41f4b71Sopenharmony_ciputSync(key: string, value: ValueType): void 1512e41f4b71Sopenharmony_ci 1513e41f4b71Sopenharmony_ci将数据写入缓存的Preferences实例中,可通过[flush](#flush)将Preferences实例持久化,此为同步接口。 1514e41f4b71Sopenharmony_ci 1515e41f4b71Sopenharmony_ci > **说明:** 1516e41f4b71Sopenharmony_ci > 1517e41f4b71Sopenharmony_ci > 当对应的键已经存在时,putSync()方法会覆盖其值。可以使用hasSync()方法检查是否存在对应键值对。 1518e41f4b71Sopenharmony_ci 1519e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1520e41f4b71Sopenharmony_ci 1521e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1522e41f4b71Sopenharmony_ci 1523e41f4b71Sopenharmony_ci**参数:** 1524e41f4b71Sopenharmony_ci 1525e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1526e41f4b71Sopenharmony_ci| ------ | ----------------------- | ---- | ------------------------ | 1527e41f4b71Sopenharmony_ci| key | string | 是 | 要修改的存储的Key,不能为空。 | 1528e41f4b71Sopenharmony_ci| value | [ValueType](#valuetype) | 是 | 存储的新值。 | 1529e41f4b71Sopenharmony_ci 1530e41f4b71Sopenharmony_ci**错误码:** 1531e41f4b71Sopenharmony_ci 1532e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1533e41f4b71Sopenharmony_ci 1534e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1535e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1536e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1537e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1538e41f4b71Sopenharmony_ci 1539e41f4b71Sopenharmony_ci**示例:** 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ci```ts 1542e41f4b71Sopenharmony_cidataPreferences.putSync('startup', 'auto'); 1543e41f4b71Sopenharmony_ci``` 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ci 1546e41f4b71Sopenharmony_ci### has 1547e41f4b71Sopenharmony_ci 1548e41f4b71Sopenharmony_cihas(key: string, callback: AsyncCallback<boolean>): void 1549e41f4b71Sopenharmony_ci 1550e41f4b71Sopenharmony_ci检查缓存的Preferences实例中是否包含名为给定Key的存储键值对,使用callback异步回调。 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1553e41f4b71Sopenharmony_ci 1554e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1555e41f4b71Sopenharmony_ci 1556e41f4b71Sopenharmony_ci**参数:** 1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1559e41f4b71Sopenharmony_ci| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | 1560e41f4b71Sopenharmony_ci| key | string | 是 | 要检查的存储key名称,不能为空。 | 1561e41f4b71Sopenharmony_ci| callback | AsyncCallback<boolean> | 是 | 回调函数。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 | 1562e41f4b71Sopenharmony_ci 1563e41f4b71Sopenharmony_ci**错误码:** 1564e41f4b71Sopenharmony_ci 1565e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1566e41f4b71Sopenharmony_ci 1567e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1568e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1569e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1570e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1571e41f4b71Sopenharmony_ci 1572e41f4b71Sopenharmony_ci**示例:** 1573e41f4b71Sopenharmony_ci 1574e41f4b71Sopenharmony_ci```ts 1575e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1576e41f4b71Sopenharmony_ci 1577e41f4b71Sopenharmony_cidataPreferences.has('startup', (err: BusinessError, val: boolean) => { 1578e41f4b71Sopenharmony_ci if (err) { 1579e41f4b71Sopenharmony_ci console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); 1580e41f4b71Sopenharmony_ci return; 1581e41f4b71Sopenharmony_ci } 1582e41f4b71Sopenharmony_ci if (val) { 1583e41f4b71Sopenharmony_ci console.info("The key 'startup' is contained."); 1584e41f4b71Sopenharmony_ci } else { 1585e41f4b71Sopenharmony_ci console.info("The key 'startup' dose not contain."); 1586e41f4b71Sopenharmony_ci } 1587e41f4b71Sopenharmony_ci}) 1588e41f4b71Sopenharmony_ci``` 1589e41f4b71Sopenharmony_ci 1590e41f4b71Sopenharmony_ci 1591e41f4b71Sopenharmony_ci### has 1592e41f4b71Sopenharmony_ci 1593e41f4b71Sopenharmony_cihas(key: string): Promise<boolean> 1594e41f4b71Sopenharmony_ci 1595e41f4b71Sopenharmony_ci检查缓存的Preferences实例中是否包含名为给定Key的存储键值对,使用Promise异步回调。 1596e41f4b71Sopenharmony_ci 1597e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1598e41f4b71Sopenharmony_ci 1599e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1600e41f4b71Sopenharmony_ci 1601e41f4b71Sopenharmony_ci**参数:** 1602e41f4b71Sopenharmony_ci 1603e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1604e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------- | 1605e41f4b71Sopenharmony_ci| key | string | 是 | 要检查的存储key名称,不能为空。 | 1606e41f4b71Sopenharmony_ci 1607e41f4b71Sopenharmony_ci**返回值:** 1608e41f4b71Sopenharmony_ci 1609e41f4b71Sopenharmony_ci| 类型 | 说明 | 1610e41f4b71Sopenharmony_ci| ---------------------- | ------------------------------------------------------------ | 1611e41f4b71Sopenharmony_ci| Promise<boolean> | Promise对象。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 | 1612e41f4b71Sopenharmony_ci 1613e41f4b71Sopenharmony_ci**错误码:** 1614e41f4b71Sopenharmony_ci 1615e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1616e41f4b71Sopenharmony_ci 1617e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1618e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1619e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1620e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1621e41f4b71Sopenharmony_ci 1622e41f4b71Sopenharmony_ci**示例:** 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci```ts 1625e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1626e41f4b71Sopenharmony_ci 1627e41f4b71Sopenharmony_cilet promise = dataPreferences.has('startup'); 1628e41f4b71Sopenharmony_cipromise.then((val: boolean) => { 1629e41f4b71Sopenharmony_ci if (val) { 1630e41f4b71Sopenharmony_ci console.info("The key 'startup' is contained."); 1631e41f4b71Sopenharmony_ci } else { 1632e41f4b71Sopenharmony_ci console.info("The key 'startup' dose not contain."); 1633e41f4b71Sopenharmony_ci } 1634e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1635e41f4b71Sopenharmony_ci console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); 1636e41f4b71Sopenharmony_ci}) 1637e41f4b71Sopenharmony_ci``` 1638e41f4b71Sopenharmony_ci 1639e41f4b71Sopenharmony_ci 1640e41f4b71Sopenharmony_ci### hasSync<sup>10+</sup> 1641e41f4b71Sopenharmony_ci 1642e41f4b71Sopenharmony_cihasSync(key: string): boolean 1643e41f4b71Sopenharmony_ci 1644e41f4b71Sopenharmony_ci检查缓存的Preferences实例中是否包含名为给定Key的存储键值对,此为同步接口。 1645e41f4b71Sopenharmony_ci 1646e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1647e41f4b71Sopenharmony_ci 1648e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1649e41f4b71Sopenharmony_ci 1650e41f4b71Sopenharmony_ci**参数:** 1651e41f4b71Sopenharmony_ci 1652e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1653e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------- | 1654e41f4b71Sopenharmony_ci| key | string | 是 | 要检查的存储key名称,不能为空。 | 1655e41f4b71Sopenharmony_ci 1656e41f4b71Sopenharmony_ci**返回值:** 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ci| 类型 | 说明 | 1659e41f4b71Sopenharmony_ci| ---------------------- | ------------------------------------------------------------ | 1660e41f4b71Sopenharmony_ci| boolean | 返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 | 1661e41f4b71Sopenharmony_ci 1662e41f4b71Sopenharmony_ci**错误码:** 1663e41f4b71Sopenharmony_ci 1664e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1665e41f4b71Sopenharmony_ci 1666e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1667e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1668e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1669e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1670e41f4b71Sopenharmony_ci 1671e41f4b71Sopenharmony_ci**示例:** 1672e41f4b71Sopenharmony_ci 1673e41f4b71Sopenharmony_ci```ts 1674e41f4b71Sopenharmony_cilet isExist: boolean = dataPreferences.hasSync('startup'); 1675e41f4b71Sopenharmony_ciif (isExist) { 1676e41f4b71Sopenharmony_ci console.info("The key 'startup' is contained."); 1677e41f4b71Sopenharmony_ci} else { 1678e41f4b71Sopenharmony_ci console.info("The key 'startup' dose not contain."); 1679e41f4b71Sopenharmony_ci} 1680e41f4b71Sopenharmony_ci``` 1681e41f4b71Sopenharmony_ci 1682e41f4b71Sopenharmony_ci 1683e41f4b71Sopenharmony_ci### delete 1684e41f4b71Sopenharmony_ci 1685e41f4b71Sopenharmony_cidelete(key: string, callback: AsyncCallback<void>): void 1686e41f4b71Sopenharmony_ci 1687e41f4b71Sopenharmony_ci从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。 1688e41f4b71Sopenharmony_ci 1689e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1690e41f4b71Sopenharmony_ci 1691e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1692e41f4b71Sopenharmony_ci 1693e41f4b71Sopenharmony_ci**参数:** 1694e41f4b71Sopenharmony_ci 1695e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1696e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------------------- | 1697e41f4b71Sopenharmony_ci| key | string | 是 | 要删除的存储Key名称,不能为空。 | 1698e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功,err为undefined;否则为错误对象。 | 1699e41f4b71Sopenharmony_ci 1700e41f4b71Sopenharmony_ci**错误码:** 1701e41f4b71Sopenharmony_ci 1702e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1703e41f4b71Sopenharmony_ci 1704e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1705e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1706e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1707e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ci**示例:** 1710e41f4b71Sopenharmony_ci 1711e41f4b71Sopenharmony_ci```ts 1712e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1713e41f4b71Sopenharmony_ci 1714e41f4b71Sopenharmony_cidataPreferences.delete('startup', (err: BusinessError) => { 1715e41f4b71Sopenharmony_ci if (err) { 1716e41f4b71Sopenharmony_ci console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); 1717e41f4b71Sopenharmony_ci return; 1718e41f4b71Sopenharmony_ci } 1719e41f4b71Sopenharmony_ci console.info("Succeeded in deleting the key 'startup'."); 1720e41f4b71Sopenharmony_ci}) 1721e41f4b71Sopenharmony_ci``` 1722e41f4b71Sopenharmony_ci 1723e41f4b71Sopenharmony_ci 1724e41f4b71Sopenharmony_ci### delete 1725e41f4b71Sopenharmony_ci 1726e41f4b71Sopenharmony_cidelete(key: string): Promise<void> 1727e41f4b71Sopenharmony_ci 1728e41f4b71Sopenharmony_ci从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。 1729e41f4b71Sopenharmony_ci 1730e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1731e41f4b71Sopenharmony_ci 1732e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1733e41f4b71Sopenharmony_ci 1734e41f4b71Sopenharmony_ci**参数:** 1735e41f4b71Sopenharmony_ci 1736e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1737e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------- | 1738e41f4b71Sopenharmony_ci| key | string | 是 | 要删除的存储key名称,不能为空。 | 1739e41f4b71Sopenharmony_ci 1740e41f4b71Sopenharmony_ci**返回值:** 1741e41f4b71Sopenharmony_ci 1742e41f4b71Sopenharmony_ci| 类型 | 说明 | 1743e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 1744e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 1745e41f4b71Sopenharmony_ci 1746e41f4b71Sopenharmony_ci**错误码:** 1747e41f4b71Sopenharmony_ci 1748e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1749e41f4b71Sopenharmony_ci 1750e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1751e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1752e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1753e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1754e41f4b71Sopenharmony_ci 1755e41f4b71Sopenharmony_ci**示例:** 1756e41f4b71Sopenharmony_ci 1757e41f4b71Sopenharmony_ci```ts 1758e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_cilet promise = dataPreferences.delete('startup'); 1761e41f4b71Sopenharmony_cipromise.then(() => { 1762e41f4b71Sopenharmony_ci console.info("Succeeded in deleting the key 'startup'."); 1763e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1764e41f4b71Sopenharmony_ci console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); 1765e41f4b71Sopenharmony_ci}) 1766e41f4b71Sopenharmony_ci``` 1767e41f4b71Sopenharmony_ci 1768e41f4b71Sopenharmony_ci 1769e41f4b71Sopenharmony_ci### deleteSync<sup>10+</sup> 1770e41f4b71Sopenharmony_ci 1771e41f4b71Sopenharmony_cideleteSync(key: string): void 1772e41f4b71Sopenharmony_ci 1773e41f4b71Sopenharmony_ci从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过[flush](#flush)将Preferences实例持久化,此为同步接口。 1774e41f4b71Sopenharmony_ci 1775e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1776e41f4b71Sopenharmony_ci 1777e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1778e41f4b71Sopenharmony_ci 1779e41f4b71Sopenharmony_ci**参数:** 1780e41f4b71Sopenharmony_ci 1781e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1782e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------- | 1783e41f4b71Sopenharmony_ci| key | string | 是 | 要删除的存储key名称,不能为空。 | 1784e41f4b71Sopenharmony_ci 1785e41f4b71Sopenharmony_ci**错误码:** 1786e41f4b71Sopenharmony_ci 1787e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1788e41f4b71Sopenharmony_ci 1789e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1790e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1791e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1792e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1793e41f4b71Sopenharmony_ci 1794e41f4b71Sopenharmony_ci**示例:** 1795e41f4b71Sopenharmony_ci 1796e41f4b71Sopenharmony_ci```ts 1797e41f4b71Sopenharmony_cidataPreferences.deleteSync('startup'); 1798e41f4b71Sopenharmony_ci``` 1799e41f4b71Sopenharmony_ci 1800e41f4b71Sopenharmony_ci 1801e41f4b71Sopenharmony_ci### flush 1802e41f4b71Sopenharmony_ci 1803e41f4b71Sopenharmony_ciflush(callback: AsyncCallback<void>): void 1804e41f4b71Sopenharmony_ci 1805e41f4b71Sopenharmony_ci将缓存的Preferences实例中的数据异步存储到用户首选项的持久化文件中,使用callback异步回调。 1806e41f4b71Sopenharmony_ci 1807e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1808e41f4b71Sopenharmony_ci 1809e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1810e41f4b71Sopenharmony_ci 1811e41f4b71Sopenharmony_ci**参数:** 1812e41f4b71Sopenharmony_ci 1813e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1814e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------------------- | 1815e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当保存成功,err为undefined;否则为错误对象。 | 1816e41f4b71Sopenharmony_ci 1817e41f4b71Sopenharmony_ci**错误码:** 1818e41f4b71Sopenharmony_ci 1819e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1820e41f4b71Sopenharmony_ci 1821e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1822e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1823e41f4b71Sopenharmony_ci| 401 | Parameter error. Mandatory parameters are left unspecified. | 1824e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1825e41f4b71Sopenharmony_ci 1826e41f4b71Sopenharmony_ci**示例:** 1827e41f4b71Sopenharmony_ci 1828e41f4b71Sopenharmony_ci```ts 1829e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1830e41f4b71Sopenharmony_ci 1831e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 1832e41f4b71Sopenharmony_ci if (err) { 1833e41f4b71Sopenharmony_ci console.error("Failed to flush. code =" + err.code + ", message =" + err.message); 1834e41f4b71Sopenharmony_ci return; 1835e41f4b71Sopenharmony_ci } 1836e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 1837e41f4b71Sopenharmony_ci}) 1838e41f4b71Sopenharmony_ci``` 1839e41f4b71Sopenharmony_ci 1840e41f4b71Sopenharmony_ci 1841e41f4b71Sopenharmony_ci### flush 1842e41f4b71Sopenharmony_ci 1843e41f4b71Sopenharmony_ciflush(): Promise<void> 1844e41f4b71Sopenharmony_ci 1845e41f4b71Sopenharmony_ci将缓存的Preferences实例中的数据异步存储到用户首选项的持久化文件中,使用Promise异步回调。 1846e41f4b71Sopenharmony_ci 1847e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1848e41f4b71Sopenharmony_ci 1849e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1850e41f4b71Sopenharmony_ci 1851e41f4b71Sopenharmony_ci**返回值:** 1852e41f4b71Sopenharmony_ci 1853e41f4b71Sopenharmony_ci| 类型 | 说明 | 1854e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 1855e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 1856e41f4b71Sopenharmony_ci 1857e41f4b71Sopenharmony_ci**错误码:** 1858e41f4b71Sopenharmony_ci 1859e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[用户首选项错误码](errorcode-preferences.md)。 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1862e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1863e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci**示例:** 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci```ts 1868e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1869e41f4b71Sopenharmony_ci 1870e41f4b71Sopenharmony_cilet promise = dataPreferences.flush(); 1871e41f4b71Sopenharmony_cipromise.then(() => { 1872e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 1873e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1874e41f4b71Sopenharmony_ci console.error("Failed to flush. code =" + err.code + ", message =" + err.message); 1875e41f4b71Sopenharmony_ci}) 1876e41f4b71Sopenharmony_ci``` 1877e41f4b71Sopenharmony_ci 1878e41f4b71Sopenharmony_ci 1879e41f4b71Sopenharmony_ci### clear 1880e41f4b71Sopenharmony_ci 1881e41f4b71Sopenharmony_ciclear(callback: AsyncCallback<void>): void 1882e41f4b71Sopenharmony_ci 1883e41f4b71Sopenharmony_ci清除缓存的Preferences实例中的所有数据,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。 1884e41f4b71Sopenharmony_ci 1885e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1886e41f4b71Sopenharmony_ci 1887e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1888e41f4b71Sopenharmony_ci 1889e41f4b71Sopenharmony_ci**参数:** 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1892e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------------------------------- | 1893e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当清除成功,err为undefined;否则为错误对象。 | 1894e41f4b71Sopenharmony_ci 1895e41f4b71Sopenharmony_ci**错误码:** 1896e41f4b71Sopenharmony_ci 1897e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1898e41f4b71Sopenharmony_ci 1899e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1900e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1901e41f4b71Sopenharmony_ci| 401 | Parameter error. Mandatory parameters are left unspecified. | 1902e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1903e41f4b71Sopenharmony_ci 1904e41f4b71Sopenharmony_ci**示例:** 1905e41f4b71Sopenharmony_ci 1906e41f4b71Sopenharmony_ci```ts 1907e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1908e41f4b71Sopenharmony_ci 1909e41f4b71Sopenharmony_cidataPreferences.clear((err: BusinessError) =>{ 1910e41f4b71Sopenharmony_ci if (err) { 1911e41f4b71Sopenharmony_ci console.error("Failed to clear. code =" + err.code + ", message =" + err.message); 1912e41f4b71Sopenharmony_ci return; 1913e41f4b71Sopenharmony_ci } 1914e41f4b71Sopenharmony_ci console.info("Succeeded in clearing."); 1915e41f4b71Sopenharmony_ci}) 1916e41f4b71Sopenharmony_ci``` 1917e41f4b71Sopenharmony_ci 1918e41f4b71Sopenharmony_ci 1919e41f4b71Sopenharmony_ci### clear 1920e41f4b71Sopenharmony_ci 1921e41f4b71Sopenharmony_ciclear(): Promise<void> 1922e41f4b71Sopenharmony_ci 1923e41f4b71Sopenharmony_ci清除缓存的Preferences实例中的所有数据,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。 1924e41f4b71Sopenharmony_ci 1925e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1926e41f4b71Sopenharmony_ci 1927e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1928e41f4b71Sopenharmony_ci 1929e41f4b71Sopenharmony_ci**返回值:** 1930e41f4b71Sopenharmony_ci 1931e41f4b71Sopenharmony_ci| 类型 | 说明 | 1932e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 1933e41f4b71Sopenharmony_ci| Promise<void> | 无返回结果的Promise对象。 | 1934e41f4b71Sopenharmony_ci 1935e41f4b71Sopenharmony_ci**错误码:** 1936e41f4b71Sopenharmony_ci 1937e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[用户首选项错误码](errorcode-preferences.md)。 1938e41f4b71Sopenharmony_ci 1939e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1940e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1941e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1942e41f4b71Sopenharmony_ci 1943e41f4b71Sopenharmony_ci**示例:** 1944e41f4b71Sopenharmony_ci 1945e41f4b71Sopenharmony_ci```ts 1946e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1947e41f4b71Sopenharmony_ci 1948e41f4b71Sopenharmony_cilet promise = dataPreferences.clear(); 1949e41f4b71Sopenharmony_cipromise.then(() => { 1950e41f4b71Sopenharmony_ci console.info("Succeeded in clearing."); 1951e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1952e41f4b71Sopenharmony_ci console.error("Failed to clear. code =" + err.code + ", message =" + err.message); 1953e41f4b71Sopenharmony_ci}) 1954e41f4b71Sopenharmony_ci``` 1955e41f4b71Sopenharmony_ci 1956e41f4b71Sopenharmony_ci 1957e41f4b71Sopenharmony_ci### clearSync<sup>10+</sup> 1958e41f4b71Sopenharmony_ci 1959e41f4b71Sopenharmony_ciclearSync(): void 1960e41f4b71Sopenharmony_ci 1961e41f4b71Sopenharmony_ci清除缓存的Preferences实例中的所有数据,可通过[flush](#flush)将Preferences实例持久化,此为同步接口。 1962e41f4b71Sopenharmony_ci 1963e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1964e41f4b71Sopenharmony_ci 1965e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1966e41f4b71Sopenharmony_ci 1967e41f4b71Sopenharmony_ci**示例:** 1968e41f4b71Sopenharmony_ci 1969e41f4b71Sopenharmony_ci```ts 1970e41f4b71Sopenharmony_cidataPreferences.clearSync(); 1971e41f4b71Sopenharmony_ci``` 1972e41f4b71Sopenharmony_ci 1973e41f4b71Sopenharmony_ci 1974e41f4b71Sopenharmony_ci### on('change') 1975e41f4b71Sopenharmony_ci 1976e41f4b71Sopenharmony_cion(type: 'change', callback: Callback<string>): void 1977e41f4b71Sopenharmony_ci 1978e41f4b71Sopenharmony_ci订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。 1979e41f4b71Sopenharmony_ci 1980e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1981e41f4b71Sopenharmony_ci 1982e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 1983e41f4b71Sopenharmony_ci 1984e41f4b71Sopenharmony_ci**参数:** 1985e41f4b71Sopenharmony_ci 1986e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1987e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ---------------------------------------- | 1988e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'change',表示数据变更。 | 1989e41f4b71Sopenharmony_ci| callback | Callback<string> | 是 | 回调函数。 | 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_ci**错误码:** 1992e41f4b71Sopenharmony_ci 1993e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 1994e41f4b71Sopenharmony_ci 1995e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1996e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 1997e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 1998e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 1999e41f4b71Sopenharmony_ci 2000e41f4b71Sopenharmony_ci**示例:** 2001e41f4b71Sopenharmony_ci 2002e41f4b71Sopenharmony_ci```ts 2003e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2004e41f4b71Sopenharmony_ci 2005e41f4b71Sopenharmony_cilet observer = (key: string) => { 2006e41f4b71Sopenharmony_ci console.info("The key " + key + " changed."); 2007e41f4b71Sopenharmony_ci} 2008e41f4b71Sopenharmony_cidataPreferences.on('change', observer); 2009e41f4b71Sopenharmony_cidataPreferences.putSync('startup', 'manual'); 2010e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2011e41f4b71Sopenharmony_ci if (err) { 2012e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2013e41f4b71Sopenharmony_ci return; 2014e41f4b71Sopenharmony_ci } 2015e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2016e41f4b71Sopenharmony_ci}) 2017e41f4b71Sopenharmony_ci``` 2018e41f4b71Sopenharmony_ci 2019e41f4b71Sopenharmony_ci### on('multiProcessChange')<sup>10+</sup> 2020e41f4b71Sopenharmony_ci 2021e41f4b71Sopenharmony_cion(type: 'multiProcessChange', callback: Callback<string>): void 2022e41f4b71Sopenharmony_ci 2023e41f4b71Sopenharmony_ci订阅进程间数据变更,多个进程持有同一个首选项文件时,订阅的Key的值在任意一个进程发生变更后,执行[flush](#flush)方法后,触发callback回调。 2024e41f4b71Sopenharmony_ci 2025e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2026e41f4b71Sopenharmony_ci 2027e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2028e41f4b71Sopenharmony_ci 2029e41f4b71Sopenharmony_ci**参数:** 2030e41f4b71Sopenharmony_ci 2031e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2032e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 2033e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'multiProcessChange',表示多进程间的数据变更。 | 2034e41f4b71Sopenharmony_ci| callback | Callback<string> | 是 | 回调函数。 | 2035e41f4b71Sopenharmony_ci 2036e41f4b71Sopenharmony_ci**错误码:** 2037e41f4b71Sopenharmony_ci 2038e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 2039e41f4b71Sopenharmony_ci 2040e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2041e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | 2042e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2043e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 2044e41f4b71Sopenharmony_ci| 15500019 | Failed to obtain subscription service. | 2045e41f4b71Sopenharmony_ci 2046e41f4b71Sopenharmony_ci**示例:** 2047e41f4b71Sopenharmony_ci 2048e41f4b71Sopenharmony_ci```ts 2049e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2050e41f4b71Sopenharmony_ci 2051e41f4b71Sopenharmony_cilet observer = (key: string) => { 2052e41f4b71Sopenharmony_ci console.info("The key " + key + " changed."); 2053e41f4b71Sopenharmony_ci} 2054e41f4b71Sopenharmony_cidataPreferences.on('multiProcessChange', observer); 2055e41f4b71Sopenharmony_cidataPreferences.putSync('startup', 'manual'); 2056e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2057e41f4b71Sopenharmony_ci if (err) { 2058e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2059e41f4b71Sopenharmony_ci return; 2060e41f4b71Sopenharmony_ci } 2061e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2062e41f4b71Sopenharmony_ci}) 2063e41f4b71Sopenharmony_ci``` 2064e41f4b71Sopenharmony_ci 2065e41f4b71Sopenharmony_ci### on('dataChange')<sup>12+</sup> 2066e41f4b71Sopenharmony_ci 2067e41f4b71Sopenharmony_cion(type: 'dataChange', keys: Array<string>, callback: Callback<Record<string, ValueType>>): void 2068e41f4b71Sopenharmony_ci 2069e41f4b71Sopenharmony_ci精确订阅数据变更,只有被订阅的key值发生变更后,在执行[flush](#flush)方法后,触发callback回调。 2070e41f4b71Sopenharmony_ci 2071e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2072e41f4b71Sopenharmony_ci 2073e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2074e41f4b71Sopenharmony_ci 2075e41f4b71Sopenharmony_ci**参数:** 2076e41f4b71Sopenharmony_ci 2077e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2078e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2079e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'dataChange',表示精确的数据变更。 | 2080e41f4b71Sopenharmony_ci| keys | Array<string> | 是 | 需要订阅的key集合。 | 2081e41f4b71Sopenharmony_ci| callback | Callback<Record<string, [ValueType](#valuetype)>> | 是 | 回调函数。回调支持返回多个键值对,其中键为发生变更的订阅key,值为变更后的数据:支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>、Uint8Array、object类型。 | 2082e41f4b71Sopenharmony_ci 2083e41f4b71Sopenharmony_ci**错误码:** 2084e41f4b71Sopenharmony_ci 2085e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 2086e41f4b71Sopenharmony_ci 2087e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2088e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 2089e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2090e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 2091e41f4b71Sopenharmony_ci 2092e41f4b71Sopenharmony_ci**示例:** 2093e41f4b71Sopenharmony_ci 2094e41f4b71Sopenharmony_ci```ts 2095e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2096e41f4b71Sopenharmony_ci 2097e41f4b71Sopenharmony_cilet observer = (data: Record<string, preferences.ValueType>) => { 2098e41f4b71Sopenharmony_ci for (const keyValue of Object.entries(data)) { 2099e41f4b71Sopenharmony_ci console.info(`observer : ${keyValue}`) 2100e41f4b71Sopenharmony_ci } 2101e41f4b71Sopenharmony_ci console.info("The observer called.") 2102e41f4b71Sopenharmony_ci} 2103e41f4b71Sopenharmony_cilet keys = ['name', 'age'] 2104e41f4b71Sopenharmony_cidataPreferences.on('dataChange', keys, observer); 2105e41f4b71Sopenharmony_cidataPreferences.putSync('name', 'xiaohong'); 2106e41f4b71Sopenharmony_cidataPreferences.putSync('weight', 125); 2107e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2108e41f4b71Sopenharmony_ci if (err) { 2109e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2110e41f4b71Sopenharmony_ci return; 2111e41f4b71Sopenharmony_ci } 2112e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2113e41f4b71Sopenharmony_ci}) 2114e41f4b71Sopenharmony_ci``` 2115e41f4b71Sopenharmony_ci 2116e41f4b71Sopenharmony_ci### off('change') 2117e41f4b71Sopenharmony_ci 2118e41f4b71Sopenharmony_cioff(type: 'change', callback?: Callback<string>): void 2119e41f4b71Sopenharmony_ci 2120e41f4b71Sopenharmony_ci取消订阅数据变更。 2121e41f4b71Sopenharmony_ci 2122e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2123e41f4b71Sopenharmony_ci 2124e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2125e41f4b71Sopenharmony_ci 2126e41f4b71Sopenharmony_ci**参数:** 2127e41f4b71Sopenharmony_ci 2128e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2129e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 2130e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'change',表示数据变更。 | 2131e41f4b71Sopenharmony_ci| callback | Callback<string> | 否 | 需要取消的回调函数,不填写则全部取消。 | 2132e41f4b71Sopenharmony_ci 2133e41f4b71Sopenharmony_ci**错误码:** 2134e41f4b71Sopenharmony_ci 2135e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 2136e41f4b71Sopenharmony_ci 2137e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2138e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 2139e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2140e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 2141e41f4b71Sopenharmony_ci 2142e41f4b71Sopenharmony_ci**示例:** 2143e41f4b71Sopenharmony_ci 2144e41f4b71Sopenharmony_ci```ts 2145e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2146e41f4b71Sopenharmony_ci 2147e41f4b71Sopenharmony_cilet observer = (key: string) => { 2148e41f4b71Sopenharmony_ci console.info("The key " + key + " changed."); 2149e41f4b71Sopenharmony_ci} 2150e41f4b71Sopenharmony_cidataPreferences.on('change', observer); 2151e41f4b71Sopenharmony_cidataPreferences.putSync('startup', 'auto'); 2152e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2153e41f4b71Sopenharmony_ci if (err) { 2154e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2155e41f4b71Sopenharmony_ci return; 2156e41f4b71Sopenharmony_ci } 2157e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2158e41f4b71Sopenharmony_ci}) 2159e41f4b71Sopenharmony_cidataPreferences.off('change', observer); 2160e41f4b71Sopenharmony_ci``` 2161e41f4b71Sopenharmony_ci 2162e41f4b71Sopenharmony_ci### off('multiProcessChange')<sup>10+</sup> 2163e41f4b71Sopenharmony_ci 2164e41f4b71Sopenharmony_cioff(type: 'multiProcessChange', callback?: Callback<string>): void 2165e41f4b71Sopenharmony_ci 2166e41f4b71Sopenharmony_ci取消订阅进程间数据变更。 2167e41f4b71Sopenharmony_ci 2168e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2169e41f4b71Sopenharmony_ci 2170e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_ci**参数:** 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2175e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 2176e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'multiProcessChange',表示多进程间的数据变更。 | 2177e41f4b71Sopenharmony_ci| callback | Callback<string> | 否 | 需要取消的回调函数,不填写则全部取消。 | 2178e41f4b71Sopenharmony_ci 2179e41f4b71Sopenharmony_ci**错误码:** 2180e41f4b71Sopenharmony_ci 2181e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 2182e41f4b71Sopenharmony_ci 2183e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2184e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 2185e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2186e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 2187e41f4b71Sopenharmony_ci 2188e41f4b71Sopenharmony_ci**示例:** 2189e41f4b71Sopenharmony_ci 2190e41f4b71Sopenharmony_ci```ts 2191e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2192e41f4b71Sopenharmony_ci 2193e41f4b71Sopenharmony_cilet observer = (key: string) => { 2194e41f4b71Sopenharmony_ci console.info("The key " + key + " changed."); 2195e41f4b71Sopenharmony_ci} 2196e41f4b71Sopenharmony_cidataPreferences.on('multiProcessChange', observer); 2197e41f4b71Sopenharmony_cidataPreferences.putSync('startup', 'auto'); 2198e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2199e41f4b71Sopenharmony_ci if (err) { 2200e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2201e41f4b71Sopenharmony_ci return; 2202e41f4b71Sopenharmony_ci } 2203e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2204e41f4b71Sopenharmony_ci}) 2205e41f4b71Sopenharmony_cidataPreferences.off('multiProcessChange', observer); 2206e41f4b71Sopenharmony_ci``` 2207e41f4b71Sopenharmony_ci### off('dataChange')<sup>12+</sup> 2208e41f4b71Sopenharmony_ci 2209e41f4b71Sopenharmony_cioff(type: 'dataChange', keys: Array<string>, callback?: Callback<Record<string, ValueType>>): void 2210e41f4b71Sopenharmony_ci 2211e41f4b71Sopenharmony_ci取消精确订阅数据变更。 2212e41f4b71Sopenharmony_ci 2213e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2214e41f4b71Sopenharmony_ci 2215e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci**参数:** 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2220e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2221e41f4b71Sopenharmony_ci| type | string | 是 | 事件类型,固定值'dataChange',表示精确的数据变更。 | 2222e41f4b71Sopenharmony_ci| keys | Array<string> | 是 | 需要取消订阅的key集合,当keys为空数组时,表示取消订阅全部key;当keys为非空数组时,表示只取消订阅key集合中的key。 | 2223e41f4b71Sopenharmony_ci| callback | Callback<Record<string, [ValueType](#valuetype)>> | 否 | 需要取消的回调函数,若callback不填写,表示所有的callback都需要处理;若callback填写,表示只处理该callback。 | 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**错误码:** 2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[用户首选项错误码](errorcode-preferences.md)。 2228e41f4b71Sopenharmony_ci 2229e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2230e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 2231e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2. Incorrect parameter types;<br>3. Parameter verification failed. | 2232e41f4b71Sopenharmony_ci| 15500000 | Inner error. | 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci**示例:** 2235e41f4b71Sopenharmony_ci 2236e41f4b71Sopenharmony_ci```ts 2237e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2238e41f4b71Sopenharmony_ci 2239e41f4b71Sopenharmony_cilet observer = (data: Record<string, preferences.ValueType>) => { 2240e41f4b71Sopenharmony_ci for (const keyValue of Object.entries(data)) { 2241e41f4b71Sopenharmony_ci console.info(`observer : ${keyValue}`) 2242e41f4b71Sopenharmony_ci } 2243e41f4b71Sopenharmony_ci console.info("The observer called.") 2244e41f4b71Sopenharmony_ci} 2245e41f4b71Sopenharmony_cilet keys = ['name', 'age'] 2246e41f4b71Sopenharmony_cidataPreferences.on('dataChange', keys, observer); 2247e41f4b71Sopenharmony_cidataPreferences.putSync('name', 'xiaohong'); 2248e41f4b71Sopenharmony_cidataPreferences.putSync('weight', 125); 2249e41f4b71Sopenharmony_cidataPreferences.flush((err: BusinessError) => { 2250e41f4b71Sopenharmony_ci if (err) { 2251e41f4b71Sopenharmony_ci console.error("Failed to flush. Cause: " + err); 2252e41f4b71Sopenharmony_ci return; 2253e41f4b71Sopenharmony_ci } 2254e41f4b71Sopenharmony_ci console.info("Succeeded in flushing."); 2255e41f4b71Sopenharmony_ci}) 2256e41f4b71Sopenharmony_cidataPreferences.off('dataChange', keys, observer); 2257e41f4b71Sopenharmony_ci``` 2258e41f4b71Sopenharmony_ci 2259e41f4b71Sopenharmony_ci## ValueType 2260e41f4b71Sopenharmony_ci 2261e41f4b71Sopenharmony_citype ValueType = number | string | boolean | Array\<number> | Array\<string> | Array\<boolean> | Uint8Array | object | bigint 2262e41f4b71Sopenharmony_ci 2263e41f4b71Sopenharmony_ci用于表示允许的数据字段类型。 2264e41f4b71Sopenharmony_ci 2265e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2266e41f4b71Sopenharmony_ci 2267e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core 2268e41f4b71Sopenharmony_ci 2269e41f4b71Sopenharmony_ci| 类型 | 说明 | 2270e41f4b71Sopenharmony_ci|--------------------------|-------------------| 2271e41f4b71Sopenharmony_ci| number | 表示值类型为数字。 | 2272e41f4b71Sopenharmony_ci| string | 表示值类型为字符串。 | 2273e41f4b71Sopenharmony_ci| boolean | 表示值类型为布尔值。 | 2274e41f4b71Sopenharmony_ci| Array\<number> | 表示值类型为数字类型的数组。 | 2275e41f4b71Sopenharmony_ci| Array\<boolean> | 表示值类型为布尔类型的数组。 | 2276e41f4b71Sopenharmony_ci| Array\<string> | 表示值类型为字符串类型的数组。 | 2277e41f4b71Sopenharmony_ci| Uint8Array<sup>11+</sup> | 表示值类型为8位无符号整型的数组。 | 2278e41f4b71Sopenharmony_ci| object<sup>12+</sup> | 表示值类型为对象。 | 2279e41f4b71Sopenharmony_ci| bigint<sup>12+</sup> | 表示值类型为任意精度格式的整数。 | 2280