diff --git a/zh-cn/application-dev/database/database-preference-guidelines.md b/zh-cn/application-dev/database/database-preference-guidelines.md index 9b23a2b9712f4ff200018ec7f27948a16365d95a..fa48561d2593c1176682e98299a0c714324d2c1f 100644 --- a/zh-cn/application-dev/database/database-preference-guidelines.md +++ b/zh-cn/application-dev/database/database-preference-guidelines.md @@ -55,15 +55,15 @@ | ----------- | ----------------------- | ------------------------------------------- | | Preferences | flush(): Promise\ | 将Preferences实例通过异步线程回写入文件中。 | -### 订阅数据变化 +### 订阅数据变更 -订阅数据变更者类,订阅的Key的值发生变更后,在执行flush方法后,会触发callback回调。 +订阅数据变更,订阅的Key的值发生变更后,在执行flush方法后,会触发callback回调。 **表5** 首选项变化订阅接口 | 类名 | 接口名 | 描述 | | ----------- | ------------------------------------------------------------ | -------------- | -| Preferences | on(type: 'change', callback: Callback<{ key : string }>): void | 订阅数据变化。 | +| Preferences | on(type: 'change', callback: Callback<{ key : string }>): void | 订阅数据变更。 | | Preferences | off(type: 'change', callback: Callback<{ key : string }>): void | 注销订阅。 | ### 删除数据文件 @@ -88,8 +88,30 @@ 2. 获取Preferences实例。 读取指定文件,将数据加载到Preferences实例,用于数据操作。 + + FA模型示例: + ```js - let promise = data_preferences.getPreferences(this.context, 'mystore'); + // 获取context + import featureAbility from '@ohos.ability.featureAbility' + var context = featureAbility.getContext() + + let promise = data_preferences.getPreferences(context, 'mystore'); + ``` + + Stage模型示例: + + ```ts + // 获取context + import Ability from '@ohos.application.Ability' + var context + class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } + } + + let promise = data_preferences.getPreferences(context, 'mystore'); ``` 3. 存入数据。 @@ -134,9 +156,9 @@ preferences.flush(); ``` -6. 订阅数据变化。 +6. 订阅数据变更。 - 应用订阅数据变化需要指定observer作为回调方法。订阅的Key的值发生变更后,当执行flush方法时,observer被触发回调。 + 应用订阅数据变更需要指定observer作为回调方法。订阅的Key的值发生变更后,当执行flush方法时,observer被触发回调。 ```js var observer = function (key) { 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 810ef52fcd2d13a242672da94b5dd24ed07b7f62..1be6efe3627e889d22dc4455b8368e9c150d1a10 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 @@ -38,15 +38,21 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误码。 | **示例:** +FA模型示例: + ```js +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + var preferences = null; -data_preferences.getPreferences(this.context, 'mystore', function (err, object) { +data_preferences.getPreferences(context, 'mystore', function (err, object) { if (err) { console.info("Failed to get preferences. Cause: " + err); return; @@ -56,6 +62,28 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, object) }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context; +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +var preferences = null; +data_preferences.getPreferences(context, 'mystore', function (err, object) { + if (err) { + console.info("Failed to get preferences. Cause: " + err); + return; + } + preferences = object; + console.info("Succeeded in getting preferences."); +}) +``` ## data_preferences.getPreferences @@ -69,7 +97,7 @@ getPreferences(context: Context, name: string): Promise<Preferences> | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------- | ---- | ----------------------- | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | **返回值:** @@ -80,9 +108,15 @@ getPreferences(context: Context, name: string): Promise<Preferences> **示例:** +FA模型示例: + ```js +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + var preferences = null; -let promise = data_preferences.getPreferences(this.context, 'mystore') +let promise = data_preferences.getPreferences(context, 'mystore') promise.then((object) => { preferences = object; console.info("Succeeded in getting preferences."); @@ -91,6 +125,27 @@ promise.then((object) => { }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context; +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +var preferences = null; +let promise = data_preferences.getPreferences(context, 'mystore') +promise.then((object) => { + preferences = object; + console.info("Succeeded in getting preferences."); +}).catch((err) => { + console.info("Failed to get preferences. Cause: " + err); +}) +``` ## data_preferences.deletePreferences @@ -108,14 +163,20 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | | callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 | **示例:** +FA模型示例: + ```js -data_preferences.deletePreferences(this.context, 'mystore', function (err) { +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + +data_preferences.deletePreferences(context, 'mystore', function (err) { if (err) { console.info("Failed to delete preferences. Cause: " + err); return @@ -124,6 +185,26 @@ data_preferences.deletePreferences(this.context, 'mystore', function (err) { }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +data_preferences.deletePreferences(context, 'mystore', function (err) { + if (err) { + console.info("Failed to delete preferences. Cause: " + err); + return + } + console.info("Succeeded in deleting preferences." ); +}) +``` ## data_preferences.deletePreferences @@ -141,7 +222,7 @@ deletePreferences(context: Context, name: string): Promise<void> | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------- | ---- | ----------------------- | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | **返回值:** @@ -152,8 +233,14 @@ deletePreferences(context: Context, name: string): Promise<void> **示例:** +FA模型示例: + ```js -let promise = data_preferences.deletePreferences(this.context, 'mystore') +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + +let promise = data_preferences.deletePreferences(context, 'mystore') promise.then(() => { console.info("Succeeded in deleting preferences."); }).catch((err) => { @@ -161,6 +248,25 @@ promise.then(() => { }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +let promise = data_preferences.deletePreferences(context, 'mystore') +promise.then(() => { + console.info("Succeeded in deleting preferences."); +}).catch((err) => { + console.info("Failed to delete preferences. Cause: " + err); +}) +``` ## data_preferences.removePreferencesFromCache @@ -176,14 +282,20 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------- | ---- | ---------------------------------------------------- | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | | callback | AsyncCallback<void> | 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 | **示例:** +FA模型示例: + ```js -data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + +data_preferences.removePreferencesFromCache(context, 'mystore', function (err) { if (err) { console.info("Failed to remove preferences. Cause: " + err); return; @@ -192,6 +304,26 @@ data_preferences.removePreferencesFromCache(this.context, 'mystore', function (e }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +data_preferences.removePreferencesFromCache(context, 'mystore', function (err) { + if (err) { + console.info("Failed to remove preferences. Cause: " + err); + return; + } + console.info("Succeeded in removing preferences."); +}) +``` ## data_preferences.removePreferencesFromCache @@ -207,7 +339,7 @@ removePreferencesFromCache(context: Context, name: string): Promise<void> | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------- | ---- | ----------------------- | -| context | [Context](js-apis-ability-context.md) | 是 | 应用上下文。 | +| context | Context | 是 | 应用上下文。
FA模型的应用Context定义见[Context](js-apis-Context.md)。
Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。 | | name | string | 是 | Preferences实例的名称。 | **返回值:** @@ -218,8 +350,14 @@ removePreferencesFromCache(context: Context, name: string): Promise<void> **示例:** +FA模型示例: + ```js -let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore') +// 获取context +import featureAbility from '@ohos.ability.featureAbility' +var context = featureAbility.getContext() + +let promise = data_preferences.removePreferencesFromCache(context, 'mystore') promise.then(() => { console.info("Succeeded in removing preferences."); }).catch((err) => { @@ -227,6 +365,25 @@ promise.then(() => { }) ``` +Stage模型示例: + +```ts +// 获取context +import Ability from '@ohos.application.Ability' +var context +class MainAbility extends Ability{ + onWindowStageCreate(windowStage){ + context = this.context + } +} + +let promise = data_preferences.removePreferencesFromCache(context, 'mystore') +promise.then(() => { + console.info("Succeeded in removing preferences."); +}).catch((err) => { + console.info("Failed to remove preferences. Cause: " + err); +}) +``` ## Preferences