dataPreferences.getPreferences(this.context,'mystore',(err, preferences)=>{
if(err){
console.error(`Failed to get preferences. Code:${
err.code},message:${
err.message}`);return;}console.info('Succeeded in getting preferences.');// 进行相关数据操作})
数据操作
写入数据,如果已经存在则会覆盖,可利用.has() 判断是否存在
preferences.put('startup','auto',(err)=>{
if(err){
console.error(`Failed to put data. Code:${
err.code}, message:${
err.message}`);return;}console.info('Succeeded in putting data.');})
读取数据,如果值为null或者非默认值类型,则返回默认数据。
preferences.get('startup','default',(err, val)=>{
if(err){
console.error(`Failed to get value of 'startup'. Code:${
err.code}, message:${
err.message}`);return;}console.info(`Succeeded in getting value of 'startup'. val: ${
val}.`);})
删除数据
preferences.delete('startup',(err)=>{
if(err){
console.error(`Failed to delete the key 'startup'. Code:${
err.code}, message:${
err.message}`);return;}console.info("Succeeded in deleting the key 'startup'.");})
数据持久化,应用存入数据到Preferences实例后,可以使用flush()方法实现数据持久化
preferences.flush((err)=>{
if(err){
console.error(`Failed to flush. Code:${
err.code}, message:${
err.message}`);return;}console.info('Succeeded in flushing.');})