diff --git a/zh-cn/application-dev/database/database-distributedobject-guidelines.md b/zh-cn/application-dev/database/database-distributedobject-guidelines.md index 0a441dc09b9d6c29b5d3e663e1fb0f5132c09833..3a7a8883ecd37495466335f059ac9bb14d041c75 100644 --- a/zh-cn/application-dev/database/database-distributedobject-guidelines.md +++ b/zh-cn/application-dev/database/database-distributedobject-guidelines.md @@ -142,7 +142,7 @@ function grantPermission() { let permissions = ['ohos.permission.DISTRIBUTED_DATASYNC']; context.requestPermissionsFromUser(permissions).then((data) => { - console.log('success: ${data}'); + console.info('success: ${data}'); }).catch((error) => { console.error('failed: ${error}'); }); diff --git a/zh-cn/application-dev/database/database-preference-guidelines.md b/zh-cn/application-dev/database/database-preference-guidelines.md index 2c7a5bfbc24291f6471b879daaa17327858b1633..b49c590898ebf0a1b631f89ae6b2581ff9ad6d73 100644 --- a/zh-cn/application-dev/database/database-preference-guidelines.md +++ b/zh-cn/application-dev/database/database-preference-guidelines.md @@ -114,21 +114,19 @@ ```ts // 获取context import UIAbility from '@ohos.app.ability.UIAbility'; - let context = null; let preferences = null; export default class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; + onWindowStageCreate(windowStage) { + let promise = data_preferences.getPreferences(this.context, 'mystore'); + promise.then((pref) => { + preferences = pref; + }).catch((err) => { + console.info("Failed to get preferences."); + }) } } - let promise = data_preferences.getPreferences(context, 'mystore'); - promise.then((pref) => { - preferences = pref; - }).catch((err) => { - console.info("Failed to get preferences."); - }) ``` 3. 存入数据。 diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md b/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md index e7a6f1413b2bd9983151e4bee9b324499fbe832f..93a97b17a6562534741231e0170108f6e4ae740d 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md @@ -15,7 +15,7 @@ import distributedObject from '@ohos.data.distributedDataObject'; ## distributedObject.create9+ -create(context: Context, source: object): DistributedObjectV9 +create(context: Context, source: object): DataObject 创建一个分布式数据对象。 @@ -32,7 +32,7 @@ create(context: Context, source: object): DistributedObjectV9 | 类型 | 说明 | | -------- | -------- | -| [DistributedObjectV9](#distributedobjectv9) | 创建完成的分布式数据对象。 | +| [DataObject](#dataobject) | 创建完成的分布式数据对象。 | **示例:** @@ -44,7 +44,7 @@ import distributedObject from '@ohos.data.distributedDataObject'; import featureAbility from '@ohos.ability.featureAbility'; // 获取context let context = featureAbility.getContext(); -// 创建对象,该对象包含4个属性类型,string,number,boolean和Object +// 创建对象,该对象包含4个属性类型:string、number、boolean和Object let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); ``` @@ -55,15 +55,14 @@ Stage模型示例: import distributedObject from '@ohos.data.distributedDataObject'; import UIAbility from '@ohos.app.ability.UIAbility'; -// 获取context -let context; +let g_object = null; + class EntryAbility extends UIAbility { onWindowStageCreate(windowStage){ - context = this.context + // 创建对象,该对象包含4个属性类型:string、number、boolean和Object + g_object = distributedObject.create(this.context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); } } -// 创建对象,该对象包含4个属性类型,string,number,boolean和Object -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); ``` ## distributedObject.genSessionId @@ -109,9 +108,9 @@ revokeSave接口回调信息。 | -------- | -------- | -------- | -------- | | sessionId | string | 是 | 多设备协同的唯一标识。 | -## DistributedObjectV9 +## DataObject -表示一个分布式数据对象。 +表示一个分布式数据对象。在使用以下接口前,需调用[create()](#distributedobjectcreate9)获取DataObject对象。 ### setSessionId9+ @@ -140,36 +139,11 @@ setSessionId(sessionId: string, callback: AsyncCallback<void>): void **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; // g_object加入分布式组网 g_object.setSessionId(distributedObject.genSessionId(), ()=>{ - console.log("join session"); -}); -``` -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -// g_object加入分布式组网 -g_object.setSessionId(distributedObject.genSessionId(), ()=>{ - console.log("join session"); + console.info("join session"); }); ``` @@ -199,44 +173,15 @@ setSessionId(callback: AsyncCallback<void>): void **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; // g_object加入分布式组网 g_object.setSessionId(distributedObject.genSessionId(), ()=>{ - console.log("join session"); + console.info("join session"); }); // 退出分布式组网 g_object.setSessionId(() => { - console.log("leave all lession."); -}); -``` -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -// g_object加入分布式组网 -g_object.setSessionId(distributedObject.genSessionId(), ()=>{ - console.log("join session"); -}); -// 退出分布式组网 -g_object.setSessionId(() => { - console.log("leave all lession."); + console.info("leave all lession."); }); ``` @@ -278,35 +223,7 @@ FA模型示例 import distributedObject from '@ohos.data.distributedDataObject'; import featureAbility from '@ohos.ability.featureAbility'; // 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -// g_object加入分布式组网 -g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ - console.log("join session."); - }).catch((error)=>{ - console.info("error:" + error.code + error.message); -}); -// 退出分布式组网 -g_object.setSessionId().then (()=>{ - console.log("leave all lession."); - }).catch((error)=>{ - console.info("error:" + error.code + error.message); -}); -``` -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; // g_object加入分布式组网 g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ console.info("join session."); @@ -315,7 +232,7 @@ g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ }); // 退出分布式组网 g_object.setSessionId().then (()=>{ - console.log("leave all lession."); + console.info("leave all lession."); }).catch((error)=>{ console.info("error:" + error.code + error.message); }); @@ -338,39 +255,8 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -globalThis.changeCallback = (sessionId, changeData) => { - console.info("change" + sessionId); - if (changeData != null && changeData != undefined) { - changeData.forEach(element => { - console.info("changed !" + element + " " + g_object[element]); - }); - } -} -g_object.on("change", globalThis.changeCallback); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; globalThis.changeCallback = (sessionId, changeData) => { console.info("change" + sessionId); if (changeData != null && changeData != undefined) { @@ -400,34 +286,8 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -// 删除数据变更回调changeCallback -g_object.off("change", globalThis.changeCallback); -// 删除所有的数据变更回调 -g_object.off("change"); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; // 删除数据变更回调changeCallback g_object.off("change", globalThis.changeCallback); // 删除所有的数据变更回调 @@ -451,37 +311,14 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st **示例:** -FA模型示例 - ```js import distributedObject from '@ohos.data.distributedDataObject'; import featureAbility from '@ohos.ability.featureAbility'; // 获取context -let context = featureAbility.getContext(); +let g_object; globalThis.statusCallback = (sessionId, networkId, status) => { globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; } -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -g_object.on("status", globalThis.statusCallback); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); g_object.on("status", globalThis.statusCallback); ``` @@ -503,37 +340,8 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; -} -// 删除上下线回调changeCallback -g_object.off("status",globalThis.statusCallback); -// 删除所有的上下线回调 -g_object.off("status"); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +let g_object; globalThis.statusCallback = (sessionId, networkId, status) => { globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; } @@ -568,38 +376,11 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void **示例:** -FA模型示例 -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); -g_object.setSessionId("123456"); -g_object.save("local", (result) => { - console.log("save callback"); - console.info("save sessionId: " + result.sessionId); - console.info("save version: " + result.version); - console.info("save deviceId: " + result.deviceId); -}); -``` - -Stage模型示例 ```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); +let g_object; g_object.setSessionId("123456"); g_object.save("local", (result) => { - console.log("save callback"); + console.info("save callback"); console.info("save sessionId: " + result.sessionId); console.info("save version: " + result.version); console.info("save deviceId: " + result.deviceId); @@ -640,34 +421,10 @@ save(deviceId: string): Promise<SaveSuccessResponse> import distributedObject from '@ohos.data.distributedDataObject'; import featureAbility from '@ohos.ability.featureAbility'; // 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context,{name:"Amy", age:18, isVis:false}); -g_object.setSessionId("123456"); -g_object.save("local").then((result) => { - console.log("save callback"); - console.info("save sessionId " + result.sessionId); - console.info("save version " + result.version); - console.info("save deviceId " + result.deviceId); -}, () => { - console.error("save failed"); -}); -``` - -```js -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context - } -} -let g_object = distributedObject.create(context,{name:"Amy", age:18, isVis:false}); +let g_object; g_object.setSessionId("123456"); g_object.save("local").then((result) => { - console.log("save callback"); + console.info("save callback"); console.info("save sessionId " + result.sessionId); console.info("save version " + result.version); console.info("save deviceId " + result.deviceId); @@ -695,55 +452,20 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void **示例:** -FA模型示例 - ```js -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); -g_object.setSessionId("123456"); -// 持久化数据 -g_object.save("local", (result) => { - console.log("save callback"); - console.info("save sessionId " + result.sessionId); - console.info("save version " + result.version); - console.info("save deviceId " + result.deviceId); -}); -// 删除持久化保存的数据 -g_object.revokeSave((result) => { - console.log("revokeSave callback"); - console.log("revokeSave sessionId " + result.sessionId); -}); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); +let g_object; g_object.setSessionId("123456"); // 持久化数据 g_object.save("local", (result) => { - console.log("save callback"); + console.info("save callback"); console.info("save sessionId " + result.sessionId); console.info("save version " + result.version); console.info("save deviceId " + result.deviceId); }); // 删除持久化保存的数据 g_object.revokeSave((result) => { - console.log("revokeSave callback"); - console.log("revokeSave sessionId " + result.sessionId); + console.info("revokeSave callback"); + console.info("revokeSave sessionId " + result.sessionId); }); ``` @@ -769,58 +491,21 @@ revokeSave(): Promise<RevokeSaveSuccessResponse> FA模型示例 ```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import featureAbility from '@ohos.ability.featureAbility'; -// 获取context -let context = featureAbility.getContext(); -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); +let g_object; g_object.setSessionId("123456"); // 持久化数据 g_object.save("local").then((result) => { - console.log("save callback"); - console.info("save sessionId " + result.sessionId); - console.info("save version " + result.version); - console.info("save deviceId " + result.deviceId); -}, () => { - console.error("save failed"); -}); -// 删除持久化保存的数据 -g_object.revokeSave().then((result) => { - console.log("revokeSave callback"); - console.log("sessionId" + result.sessionId); -}, () => { - console.error("revokeSave failed"); -}); -``` - -Stage模型示例 - -```ts -import distributedObject from '@ohos.data.distributedDataObject'; -import UIAbility from '@ohos.app.ability.UIAbility'; - -// 获取context -let context; -class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { - context = this.context - } -} -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false}); -g_object.setSessionId("123456"); -g_object.save("local").then((result) => { - console.log("save callback"); + console.info("save callback"); console.info("save sessionId " + result.sessionId); console.info("save version " + result.version); console.info("save deviceId " + result.deviceId); }, () => { console.error("save failed"); }); - // 删除持久化保存的数据 g_object.revokeSave().then((result) => { - console.log("revokeSave callback"); - console.log("sessionId" + result.sessionId); + console.info("revokeSave callback"); + console.info("sessionId" + result.sessionId); }, () => { console.error("revokeSave failed"); }); @@ -861,7 +546,7 @@ let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, is ## DistributedObject(deprecated) -表示一个分布式数据对象。 +表示一个分布式数据对象。在使用以下接口前,需调用[createDistributedObject()](#distributedobjectcreatedistributedobjectdeprecated)获取DistributedObject对象。 ### setSessionId(deprecated) 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 b4b9fcf9505809908638d14865765a8eb316fe68..5320d1dde02a3b85b9bd644dbe92b42b86436ce1 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 @@ -68,29 +68,25 @@ try { Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; +let preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; + onWindowStageCreate(windowStage) { + try { + data_preferences.getPreferences(this.context, 'mystore', function (err, val) { + if (err) { + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); + return; + } + console.info("Succeeded in getting preferences."); + }) + } catch (err) { + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); + } } } - -let preferences = null; -try { - data_preferences.getPreferences(context, 'mystore', function (err, val) { - if (err) { - console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); - return; - } - console.info("Succeeded in getting preferences."); - }) -} catch (err) { - console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); -} ``` ## data_preferences.getPreferences @@ -130,39 +126,35 @@ try { preferences = object; console.info("Succeeded in getting preferences."); }).catch((err) => { - console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message); + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { - console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message); + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); } ``` Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; +let preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; + onWindowStageCreate(windowStage) { + try { + let promise = data_preferences.getPreferences(this.context, 'mystore'); + promise.then((object) => { + preferences = object; + console.info("Succeeded in getting preferences."); + }).catch((err) => { + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); + }) + } catch(err) { + console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message); + } } } - -let preferences = null; -try { - let promise = data_preferences.getPreferences(context, 'mystore'); - promise.then((object) => { - preferences = object; - console.info("Succeeded in getting preferences."); - }).catch((err) => { - console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message); - }) -} catch(err) { - console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message); -} ``` ## data_preferences.deletePreferences @@ -218,27 +210,22 @@ try { Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; - class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; - } -} - -try { - data_preferences.deletePreferences(context, 'mystore', function (err, val) { - if (err) { + onWindowStageCreate(windowStage) { + try { + data_preferences.deletePreferences(this.context, 'mystore', function (err, val) { + if (err) { + console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); + return; + } + console.info("Succeeded in deleting preferences." ); + }) + } catch (err) { console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); - return; } - console.info("Succeeded in deleting preferences." ); - }) -} catch (err) { - console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); + } } ``` @@ -299,27 +286,22 @@ try { Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; - class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; + onWindowStageCreate(windowStage) { + try{ + let promise = data_preferences.deletePreferences(this.context, 'mystore'); + promise.then(() => { + console.info("Succeeded in deleting preferences."); + }).catch((err) => { + console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); + }) + } catch(err) { + console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); + } } } - -try{ - let promise = data_preferences.deletePreferences(context, 'mystore'); - promise.then(() => { - console.info("Succeeded in deleting preferences."); - }).catch((err) => { - console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); - }) -} catch(err) { - console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message); -} ``` ## data_preferences.removePreferencesFromCache @@ -365,27 +347,22 @@ try { Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; - class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; - } -} - -try { - data_preferences.removePreferencesFromCache(context, 'mystore', function (err, val) { - if (err) { + onWindowStageCreate(windowStage) { + try { + data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err, val) { + if (err) { + console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + return; + } + console.info("Succeeded in removing preferences."); + }) + } catch (err) { console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); - return; } - console.info("Succeeded in removing preferences."); - }) -} catch (err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + } } ``` @@ -437,25 +414,22 @@ try { Stage模型示例: ```ts -// 获取context import UIAbility from '@ohos.app.ability.UIAbility'; -let context = null; + class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - context = this.context; + onWindowStageCreate(windowStage) { + try { + let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore'); + promise.then(() => { + console.info("Succeeded in removing preferences."); + }).catch((err) => { + console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + }) + } catch(err) { + console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); + } } } - -try { - let promise = data_preferences.removePreferencesFromCache(context, 'mystore'); - promise.then(() => { - console.info("Succeeded in removing preferences."); - }).catch((err) => { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); - }) -} catch(err) { - console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message); -} ``` ## Preferences @@ -804,10 +778,10 @@ try { promise.then(() => { console.info("Succeeded in deleting the key 'startup'."); }).catch((err) => { - console.log("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); + console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); }) } catch(err) { - console.log("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); + console.info("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); } ```