这里面,我们首先要使用的是首选项
首选项为应用提供 Key-Value 键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。
使用的时候首先导入
导入模块
import data_preferences from '@ohos.data.preferences';
复制代码
然后获取获取 context,由于目前主推 Stage,所以这里给出 Stage 模型示例:
如下所示,我们就获取了 Preferences 实例
import UIAbility from '@ohos.app.ability.UIAbility';
let preferences = null;
class EntryAbility extends UIAbility {
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);
}
}
}
复制代码
使用完之后,我们需要移除的时候,调用如下接口就可以使用 Promise 异步回调,从内存中移除指定的 Preferences 实例。
data_preferences.deletePreferences
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
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);
}
}
}
复制代码
当我们需要使用 Promise 异步回调,从内存中移除指定的 Preferences 实例就调用下面的接口,调用该接口后,应用不允许再使用该 Preferences 实例进行数据操作,否则会出现数据一致性问题。
data_preferences.removePreferencesFromCache
import UIAbility from '@ohos.app.ability.UIAbility';
class EntryAbility extends UIAbility {
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);
}
}
}
复制代码
然后我们就可以使用 Preferences 存储实例,提供获取和修改存储数据的接口。
Preferences
get
获取键对应的值,如果值为 null 或者非默认值类型,返回默认数据 defValue,使用 Promise 异步回调。
try {
let promise = preferences.get('startup', 'default');
promise.then((data) => {
console.info("Succeeded in getting value of 'startup'. Data: " + data);
}).catch((err) => {
console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
})
} catch(err) {
console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
}
复制代码
getAll
try {
let promise = preferences.getAll();
promise.then((value) => {
let allKeys = Object.keys(value);
console.info('getAll keys = ' + allKeys);
console.info("getAll object = " + JSON.stringify(value));
}).catch((err) => {
console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
})
} catch (err) {
console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
}
复制代码
put
将数据写入 Preferences 实例,可通过flush将 Preferences 实例持久化,使用 Promise 异步回调。
()try {
let promise = preferences.put('startup', 'auto');
promise.then(() => {
console.info("Succeeded in putting value of 'startup'.");
}).catch((err) => {
console.info("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message);
})
} catch(err) {
console.info("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message);
}
复制代码
评论