diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md index 827571daedecf93ec7ba80b78565b3a12fa46f28..d13c1d5197fc30eb0d8dc4c5f37f41d7bcf10b66 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -1,52 +1,58 @@ # 首选项 -首选项为应用提供key-value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。 +首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。 + +数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。 > **说明:** +> > 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 -```ts +```js import data_preferences from '@ohos.data.preferences'; ``` ## 常量 -**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core +**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core -| 名称 | 参数类型 | 可读 | 可写 | 说明 | -| -------- | -------- | -------- | -------- | -------- | -| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,需小于80字节。 | -| MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,需小于8192字节。 | +| 名称 | 参数类型 | 可读 | 可写 | 说明 | +| ---------------- | -------- | ---- | ---- | --------------------------------------- | +| MAX_KEY_LENGTH | string | 是 | 否 | Key的最大长度限制,需小于80个字节。 | +| MAX_VALUE_LENGTH | string | 是 | 否 | Value的最大长度限制,需小于8192个字节。 | ## data_preferences.getPreferences getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void -读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用callback方式作为异步方法。 - +读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | - | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 | + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | +| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | +| callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。当获取Preferences实例成功,err为undefined,object为获取的Preferences实例;否则err为错误码。 | **示例:** -```ts -data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { + +```js +var preferences = null; +data_preferences.getPreferences(this.context, 'mystore', function (err, object) { if (err) { - console.info("Get preferences failed.") + console.info("Failed to get preferences. Cause: " + err); return; } - console.info("Get preferences successfully.") + preferences = object; + console.info("Succeeded in getting preferences."); }) ``` @@ -55,28 +61,31 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, preferen getPreferences(context: Context, name: string): Promise<Preferences> -读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用Promise方式作为异步方法。 +读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | -------------------------- | +| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 | +| 类型 | 说明 | +| ------------------------------------------ | ---------------------------------- | +| Promise<[Preferences](#preferences)> | Promise对象,返回Preferences实例。 | **示例:** -```ts + +```js +var preferences = null; let promise = data_preferences.getPreferences(this.context, 'mystore') -promise.then((preferences) => { - console.info("Get preferences successfully.") +promise.then((object) => { + preferences = object; + console.info("Succeeded in getting preferences."); }).catch((err) => { - console.info("Get preferences failed.") + console.info("Failed to get preferences. Cause: " + err); }) ``` @@ -85,26 +94,27 @@ promise.then((preferences) => { deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void -从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。 -删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。 +从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件,使用callback异步回调。 + +删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | +| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | +| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 | **示例:** -```ts +```js data_preferences.deletePreferences(this.context, 'mystore', function (err) { if (err) { - console.info("Deleted preferences failed, err: " + err) + console.info("Failed to delete preferences. Cause: " + err); return } - console.info("Deleted preferences successfully.") + console.info("Succeeded in deleting preferences." ); }) ``` @@ -113,29 +123,30 @@ data_preferences.deletePreferences(this.context, 'mystore', function (err) { deletePreferences(context: Context, name: string): Promise<void> -从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。 -删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。 +从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件,使用Promise异步回调。 + +删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | -------------------------- | +| context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步获取结果。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts +```js let promise = data_preferences.deletePreferences(this.context, 'mystore') promise.then(() => { - console.info("Deleted preferences successfully.") + console.info("Succeeded in deleting preferences."); }).catch((err) => { - console.info("Deleted preferences failed, err: " + err) + console.info("Failed to delete preferences. Cause: " + err); }) ``` @@ -144,27 +155,27 @@ promise.then(() => { removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void -从内存中移除指定首选项持久化文件对应的Preferences单实例。 +从内存中移除指定首选项持久化文件对应的Preferences单实例,使用callback异步回调。 -移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。 +移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ---------------------------------------------------- | +| context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | +| callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 | **示例:** -```ts +```js data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { if (err) { - console.info("Removed preferences from cache failed, err: " + err) - return + console.info("Failed to remove preferences. Cause: " + err); + return; } - console.info("Removed preferences from cache successfully.") + console.info("Succeeded in removing preferences."); }) ``` @@ -173,62 +184,65 @@ data_preferences.removePreferencesFromCache(this.context, 'mystore', function (e removePreferencesFromCache(context: Context, name: string): Promise<void> -从内存中移除指定首选项持久化文件对应的Preferences单实例。 +从内存中移除指定首选项持久化文件对应的Preferences单实例,使用Promise异步回调。 -移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。 +移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | - | name | string | 是 | 应用程序内部数据存储名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | -------------------------- | +| context | [Context](js-apis-ability-context.md) | 是 | 应用程序或功能的上下文。 | +| name | string | 是 | 应用程序内部数据存储名称。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步获取结果。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts +```js let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore') promise.then(() => { - console.info("Removed preferences from cache successfully.") + console.info("Succeeded in removing preferences."); }).catch((err) => { - console.info("Removed preferences from cache failed, err: " + err) + console.info("Failed to remove preferences. Cause: " + err); }) ``` ## Preferences -提供获取和修改存储数据的接口。 +存储实例,提供获取和修改存储数据的接口。 + +下列接口都需先使用[data_preferences.getPreferences](#data_preferencesgetpreferences)获取到Preferences实例,再通过此实例调用对应接口。 ### get get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void -获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用callback方式作为异步方法。 +获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------- | -------- | -------- | -| key | string | 是 | 要获取的存储key名称,不能为空。 | -| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean、Array\、Array\、Array\。 | -| callback | AsyncCallback<ValueType> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | +| key | string | 是 | 要获取的存储Key名称,不能为空。 | +| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean、Array\、Array\、Array\类型。 | +| callback | AsyncCallback<[ValueType](#valuetype)> | 是 | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误码。 | **示例:** -```ts -preferences.get('startup', 'default', function(err, value) { + +```js +preferences.get('startup', 'default', function(err, data) { if (err) { - console.info("Get value of startup failed, err: " + err) - return + console.info("Failed to get value of 'startup'. Cause: " + err); + return; } - console.info("Get value of startup is " + value) + console.info("Succeeded in getting value of 'startup'. Data: " + data); }) ``` @@ -237,28 +251,29 @@ preferences.get('startup', 'default', function(err, value) { get(key: string, defValue: ValueType): Promise<ValueType> -获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用Promise方式作为异步方法。 +获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core -- **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要获取的存储key名称,不能为空。 | - | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean、Array\、Array\、Array\。 | + **参数:** +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------ | +| key | string | 是 | 要获取的存储Key名称,不能为空。 | +| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean、Array\、Array\、Array\类型。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<ValueType> | Promise实例,用于异步获取结果。 | + +| 类型 | 说明 | +| ----------------------------------- | ----------------------------- | +| Promise<[ValueType](#valuetype)> | Promise对象,返回键对应的值。 | **示例:** -```ts -let promise = preferences.get('startup', 'default') -promise.then((value) => { - console.info("Get value of startup is " + value) +```js +let promise = preferences.get('startup', 'default'); +promise.then((data) => { + console.info("Succeeded in getting value of 'startup'. Data: " + data); }).catch((err) => { - console.info("Get value of startup failed, err: " + err) + console.info("Failed to get value of 'startup'. Cause: " + err); }) ``` @@ -266,25 +281,26 @@ promise.then((value) => { getAll(callback: AsyncCallback<Object>): void; -返回含有所有键值的Object对象。 +获取含有所有键值的Object对象。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<Object> | 是 | 回调函数。返回含有所有键值的Object对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------- | ---- | ------------------------------------------------------------ | +| callback | AsyncCallback<Object> | 是 | 回调函数。当获取成功,err为undefined,value为含有所有键值的Object对象;否则err为错误码。 | **示例:** -```ts + +```js preferences.getAll(function (err, value) { if (err) { - console.info("getAll failed, err: " + err) - return + console.info("Failed to get all key-values. Cause: " + err); + return; } - let keys = Object.keys(value) - console.info('getAll keys = ' + keys) - console.info("getAll object = " + JSON.stringify(value)) + let allKeys = Object.keys(value); + console.info("getAll keys = " + allKeys); + console.info("getAll object = " + JSON.stringify(value)); }); ``` @@ -293,24 +309,24 @@ preferences.getAll(function (err, value) { getAll(): Promise<Object> -返回含有所有键值的Object对象。 +获取含有所有键值的Object对象。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<Object> | Promise对象。返回含有所有键值的Object对象。 | +| 类型 | 说明 | +| --------------------- | ------------------------------------------- | +| Promise<Object> | Promise对象,返回含有所有键值的Object对象。 | **示例:** -```ts -let promise = preferences.getAll() +```js +let promise = preferences.getAll(); promise.then((value) => { - let keys = Object.keys(value) - console.info('getAll keys = ' + keys) - console.info("getAll object = " + JSON.stringify(value)) + let allKeys = Object.keys(value); + console.info('getAll keys = ' + allKeys); + console.info("getAll object = " + JSON.stringify(value)); }).catch((err) => { - console.info("getAll failed, err: " + err) + console.info("Failed to get all key-values. Cause: " + err); }) ``` @@ -318,25 +334,25 @@ promise.then((value) => { put(key: string, value: ValueType, callback: AsyncCallback<void>): void -首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用callback方式作为异步方法。 +将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要修改的存储的key,不能为空。 | - | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean、Array\、Array\、Array\。 | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ------------------------------------------------------------ | +| key | string | 是 | 要修改的存储的Key,不能为空。 | +| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean、Array\、Array\、Array\类型。 | +| callback | AsyncCallback<void> | 是 | 回调函数。当数据写入成功,err为undefined;否则为错误码。 | **示例:** -```ts +```js preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Put value of startup failed, err: " + err) - return + console.info("Failed to put value of 'startup'. Cause: " + err); + return; } - console.info("Put value of startup successfully.") + console.info("Succeeded in putting value of 'startup'."); }) ``` @@ -345,28 +361,28 @@ preferences.put('startup', 'auto', function (err) { put(key: string, value: ValueType): Promise<void> -首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用Promise方式作为异步方法。 +将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要修改的存储的key,不能为空。 | - | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean、Array\、Array\、Array\。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ----------------------- | ---- | ------------------------------------------------------------ | +| key | string | 是 | 要修改的存储的Key,不能为空。 | +| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean、Array\、Array\、Array\类型。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步处理。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts -let promise = preferences.put('startup', 'auto') +```js +let promise = preferences.put('startup', 'auto'); promise.then(() => { - console.info("Put value of startup successfully.") + console.info("Succeeded in putting value of 'startup'."); }).catch((err) => { - console.info("Put value of startup failed, err: " + err) + console.info("Failed to put value of 'startup'. Cause: " + err)); }) ``` @@ -375,27 +391,27 @@ promise.then(() => { has(key: string, callback: AsyncCallback<boolean>): void -检查存储对象是否包含名为给定key的存储键值对,该方法使用callback方式作为异步方法。 +检查存储对象是否包含名为给定Key的存储键值对,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要检查的存储key名称,不能为空。 | - | callback | AsyncCallback<boolean> | 是 | 回调函数。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------- | ---- | ------------------------------------------------------------ | +| key | string | 是 | 要检查的存储key名称,不能为空。 | +| callback | AsyncCallback<boolean> | 是 | 回调函数。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 | **示例:** -```ts +```js preferences.has('startup', function (err, isExist) { if (err) { - console.info("Check the key of startup failed, err: " + err) - return + console.info("Failed to check the key 'startup'. Cause: " + err); + return; } if (isExist) { - console.info("The key of startup is contained.") + console.info("The key 'startup' is contained."); } else { - console.info("The key of startup dose not contain.") + console.info("The key 'startup' dose not contain."); } }) ``` @@ -405,31 +421,32 @@ preferences.has('startup', function (err, isExist) { has(key: string): Promise<boolean> -检查存储对象是否包含名为给定key的存储键值对,该方法使用Promise方式作为异步方法。 +检查存储对象是否包含名为给定Key的存储键值对,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要检查的存储key名称,不能为空。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------ | ---- | ------------------------------- | +| key | string | 是 | 要检查的存储key名称,不能为空。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<boolean> | Promise对象。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 | +| 类型 | 说明 | +| ---------------------- | ------------------------------------------------------------ | +| Promise<boolean> | Promise对象。返回存储对象是否包含给定key的存储键值对,true表示存在,false表示不存在。 | **示例:** -```ts -let promise = preferences.has('startup') + +```js +let promise = preferences.has('startup'); promise.then((isExist) => { if (isExist) { - console.info("The key of startup is contained.") + console.info("The key 'startup' is contained."); } else { - console.info("The key of startup dose not contain.") + console.info("The key 'startup' dose not contain."); } }).catch((err) => { - console.info("Check the key of startup failed, err: " + err) + console.info("Failed to check the key 'startup'. Cause: " + err); }) ``` @@ -438,24 +455,24 @@ promise.then((isExist) => { delete(key: string, callback: AsyncCallback<void>): void -从存储对象中删除名为给定key的存储键值对,该方法使用callback方式作为异步方法。 +从存储对象中删除名为给定Key的存储键值对,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要删除的存储key名称,不能为空。 | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ---------------------------------------------------- | +| key | string | 是 | 要删除的存储Key名称,不能为空。 | +| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功,err为undefined;否则为错误码。 | **示例:** -```ts +```js preferences.delete('startup', function (err) { if (err) { - console.info("Delete startup key failed, err: " + err) - return + console.info("Failed to delete the key 'startup'. Cause: " + err); + return; } - console.info("Deleted startup key successfully.") + console.info("Succeeded in deleting the key 'startup'."); }) ``` @@ -464,27 +481,27 @@ preferences.delete('startup', function (err) { delete(key: string): Promise<void> -从存储对象删除名为给定key的存储键值对,该方法使用Promise方式作为异步方法。 +从存储对象删除名为给定Key的存储键值对,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | key | string | 是 | 要删除的存储key名称,不能为空。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------ | ---- | ------------------------------- | +| key | string | 是 | 要删除的存储key名称,不能为空。 | **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步处理。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts -let promise = preferences.delete('startup') +```js +let promise = preferences.delete('startup'); promise.then(() => { - console.info("Deleted startup key successfully.") + console.info("Succeeded in deleting the key 'startup'."); }).catch((err) => { - console.info("Delete startup key failed, err: " + err) + console.info("Failed to delete the key 'startup'. Cause: " + err); }) ``` @@ -493,23 +510,23 @@ promise.then(() => { flush(callback: AsyncCallback<void>): void -将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用callback方式作为异步方法。 +将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ---------------------------------------------------- | +| callback | AsyncCallback<void> | 是 | 回调函数。当保存成功,err为undefined;否则为错误码。 | **示例:** -```ts +```js preferences.flush(function (err) { if (err) { - console.info("Flush to file failed, err: " + err) - return + console.info("Failed to flush. Cause: " + err); + return; } - console.info("Flushed to file successfully.") + console.info("Succeeded in flushing."); }) ``` @@ -518,22 +535,22 @@ preferences.flush(function (err) { flush(): Promise<void> -将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用Promise方式作为异步方法。 +将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步处理。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts -let promise = preferences.flush() +```js +let promise = preferences.flush(); promise.then(() => { - console.info("Flushed to file successfully.") + console.info("Succeeded in flushing."); }).catch((err) => { - console.info("Flush to file failed, err: " + err) + console.info("Failed to flush. Cause: " + err); }) ``` @@ -542,23 +559,23 @@ promise.then(() => { clear(callback: AsyncCallback<void>): void -清除此存储对象中的所有存储,该方法使用callback方式作为异步方法。 +清除此存储对象中的所有存储,使用callback异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<void> | 是 | 回调函数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ---------------------------------------------------- | +| callback | AsyncCallback<void> | 是 | 回调函数。当清除成功,err为undefined;否则为错误码。 | **示例:** -```ts +```js preferences.clear(function (err) { if (err) { - console.info("Clear to file failed, err: " + err) - return + console.info("Failed to clear. Cause: " + err); + return; } - console.info("Cleared to file successfully.") + console.info("Succeeded in clearing."); }) ``` @@ -567,22 +584,22 @@ preferences.clear(function (err) { clear(): Promise<void> -清除此存储对象中的所有存储,该方法使用Promise方式作为异步方法。 +清除此存储对象中的所有存储,使用Promise异步回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **返回值:** - | 类型 | 说明 | - | -------- | -------- | - | Promise<void> | Promise实例,用于异步处理。 | +| 类型 | 说明 | +| ------------------- | ------------------------- | +| Promise<void> | 无返回结果的Promise对象。 | **示例:** -```ts +```js let promise = preferences.clear() promise.then(() => { - console.info("Cleared to file successfully.") + console.info("Succeeded in clearing."); }).catch((err) => { - console.info("Clear to file failed, err: " + err) + console.info("Failed to clear. Cause: " + err); }) ``` @@ -591,40 +608,40 @@ promise.then(() => { on(type: 'change', callback: Callback<{ key : string }>): void -订阅数据变更者类,订阅的key的值发生变更后,在执行flush方法后,callback方法会被回调。 +订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | type | string | 是 | 事件类型,固定值'change',表示数据变更。 | - | callback | Callback<{ key : string }> | 是| 回调对象实例。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------- | ---- | ---------------------------------------- | +| type | string | 是 | 事件类型,固定值'change',表示数据变更。 | +| callback | Callback<{ key : string }> | 是 | 回调对象实例。 | **示例:** -```ts +```js data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { if (err) { - console.info("Get preferences failed.") + console.info("Failed to Gget preferences."); return; } var observer = function (key) { - console.info("The key of " + key + " changed.") + console.info("The key " + key + " changed."); } - preferences.on('change', observer) + preferences.on('change', observer); preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Put the value of startup failed, err: " + err) - return + console.info("Failed to put the value of 'startup'. Cause: " + err); + return; } - console.info("Put the value of startup successfully.") + console.info("Succeeded in putting the value of 'startup'."); preferences.flush(function (err) { if (err) { - console.info("Flush to file failed, err: " + err) - return + console.info("Failed to flush. Cause: " + err); + return; } - console.info("Flushed to file successfully.") // observer will be called. + console.info("Succeeded in flushing."); // observer will be called. }) }) }) @@ -635,42 +652,42 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, preferen off(type: 'change', callback?: Callback<{ key : string }>): void -当不再进行订阅数据变更时,使用此接口取消订阅。 +取消订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。 **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **参数:** - | 参数名 | 类型 | 必填 | 说明 | - | -------- | -------- | -------- | -------- | - | type | string | 是 | 事件类型,固定值'change',表示数据变更。 | - | callback | Callback<{ key : string }> | 否| 需要取消的回调对象实例,不填则全部取消。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------- | ---- | ------------------------------------------ | +| type | string | 是 | 事件类型,固定值'change',表示数据变更。 | +| callback | Callback<{ key : string }> | 否 | 需要取消的回调对象实例,不填写则全部取消。 | **示例:** -```ts +```js data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { if (err) { - console.info("Get preferences failed.") + console.info("Failed to Gget preferences."); return; } var observer = function (key) { - console.info("The key of " + key + " changed.") + console.info("The key " + key + " changed."); } - preferences.on('change', observer) + preferences.on('change', observer); preferences.put('startup', 'auto', function (err) { if (err) { - console.info("Put the value of startup failed, err: " + err) - return + console.info("Failed to put the value of 'startup'. Cause: " + err); + return; } - console.info("Put the value of startup successfully.") + console.info("Succeeded in putting the value of 'startup'."); preferences.flush(function (err) { if (err) { - console.info("Flush to file failed, err: " + err) - return + console.info("Failed to flush. Cause: " + err); + return; } - console.info("Flushed to file successfully.") // observer will be called. + console.info("Succeeded in flushing."); // observer will be called. }) - preferences.off('change', observer) + preferences.off('change', observer); }) }) ``` @@ -681,11 +698,11 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, preferen **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core -| 类型 | 说明 | -| -------------- | ------------------------------ | -| number | 表示值类型为数字。 | -| string | 表示值类型为字符串。 | -| boolean | 表示值类型为布尔值。 | +| 类型 | 说明 | +| --------------- | ------------------------------ | +| number | 表示值类型为数字。 | +| string | 表示值类型为字符串。 | +| boolean | 表示值类型为布尔值。 | | Array\ | 表示值类型为数字类型的数组。 | | Array\ | 表示值类型为布尔类型的数组。 | -| Array\ | 表示值类型为字符串类型的数组。 | +| Array\ | 表示值类型为字符串类型的数组。 | \ No newline at end of file