代码拉取完成,页面将自动刷新
import distributedKVStore from '@ohos.data.distributedKVStore';
import common from '@ohos.app.ability.common';
import { BaseStore } from './BaseStore.interface';
import { KVStoreConstant } from '@yunzai/core'
import { BusinessError } from '@ohos.base';
export class KVManagerStore implements BaseStore {
private kvManager: distributedKVStore.KVManager | undefined = undefined;
private kvStore: distributedKVStore.SingleKVStore | undefined = undefined;
constructor(ctx: common.Context, securityLevel: distributedKVStore.SecurityLevel = distributedKVStore.SecurityLevel.S2) {
const options: distributedKVStore.Options = {
createIfMissing: true,
encrypt: true,
backup: false,
autoSync: true,
kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
securityLevel: securityLevel
}
const config: distributedKVStore.KVManagerConfig = {
context: ctx.getApplicationContext(),
bundleName: "com.yunzainfo.openharmony.primaryapp",
}
this.kvManager = distributedKVStore.createKVManager(config);
this.kvManager.getKVStore<distributedKVStore.SingleKVStore>(KVStoreConstant.STORE_ID, options, (err, store: distributedKVStore.SingleKVStore) => {
if (err) {
console.log("------------------------KV STORE ERROR--------------------------")
console.error(`Fail to get KVStore. Code:${err.code},message:${err.message}`);
return;
}
console.log("------------------------KV STORE SUCCESS--------------------------")
console.info('Succeeded in getting KVStore.');
this.kvStore = store;
});
}
async get<T>(key: string): Promise<T | undefined> {
if (!this.kvStore) return undefined
try {
const data = await this.kvStore.get(key)
if (typeof data == 'string') {
return JSON.parse(data) as T
}
} catch (e) {
let error = e as BusinessError;
if (error.code === 15100004) {
return undefined
}
}
return undefined
}
async set<T>(key: string, data: T): Promise<void> {
if (!this.kvStore) return
const json = JSON.stringify(data);
await this.kvStore.put(key, json)
}
async remove(key: string): Promise<void> {
if (!this.kvStore) return
await this.kvStore.delete(key)
}
}
当我尝试跳转
this.context.startAbility({
deviceId: "",
bundleName: "com.yunzainfo.openharmony.primaryapp",
moduleName: "oauth",
abilityName: "OauthAbility",
parameters: {
callback: "pages/Index"
}
})
在跳转到的Ability中获取KVStore中的书局时,undefined
感谢提交Issue!关于Issue的交互操作,请访问OpenHarmony社区支持命令清单。如果有问题,请联系 @NEEN @zengyawen @时睿 @Peter_1988 。如果需要调整订阅PR、Issue的变更状态,请访问链接。
Thanks for submitting the issue. For more commands, please visit OpenHarmony Command List. If you have any questions, please refer to committer @NEEN @zengyawen @时睿 @Peter_1988 for help. If you need to change the subscription of a Pull Request or Issue, please visit the link.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
import distributedKVStore from '@ohos.data.distributedKVStore';
import common from '@ohos.app.ability.common';
import { BaseStore } from './BaseStore.interface';
import { KVStoreConstant, logger } from '@yunzai/core'
import { BusinessError } from '@ohos.base';
const TAG = "KVManagerStore.ets"
export class KVManagerStore implements BaseStore {
private kvManager: distributedKVStore.KVManager | undefined = undefined;
private config: distributedKVStore.KVManagerConfig = {
bundleName: "com.yunzainfo.openharmony.primaryapp",
context: getContext(this)
}
private options: distributedKVStore.Options = {
createIfMissing: true,
encrypt: true,
backup: false,
autoSync: true,
kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
securityLevel: distributedKVStore.SecurityLevel.S2
}
constructor(ctx: common.Context) {
logger.debug(TAG, "init kvStore")
if (ctx.getApplicationContext() !== undefined && ctx.getApplicationContext() !== null) {
logger.debug(TAG, "ctx has application context")
this.config = {
context: ctx.getApplicationContext(),
bundleName: "com.yunzainfo.openharmony.primaryapp"
}
} else {
logger.debug(TAG, "ctx hasn't application context")
this.config = {
context: ctx,
bundleName: "com.yunzainfo.openharmony.primaryapp"
}
}
this.kvManager = distributedKVStore.createKVManager(this.config);
logger.debug(TAG, "kv store init over")
}
connectStore(): Promise<distributedKVStore.SingleKVStore> {
return new Promise((resolve, reject) => {
if (!this.kvManager) return
this.kvManager.getKVStore<distributedKVStore.SingleKVStore>(KVStoreConstant.STORE_ID, this.options, (err, store: distributedKVStore.SingleKVStore) => {
const isNull = (store === undefined || store === null)
logger.debug(TAG, "connect kv store", isNull ? "null" : "not null")
if (err) {
logger.debug(TAG, "connect kv store error", err.message)
reject(err)
}
logger.debug(TAG, "connect kv store success", JSON.stringify(store))
resolve(store)
});
})
}
get<T>(key: string): Promise<T | undefined> {
logger.debug(TAG, "use kv store get", key)
return new Promise((resolve, reject) => {
this.connectStore().then(store => {
store.get(key).then((data) => {
logger.debug(TAG, "use kv store get value is", data as string)
resolve(JSON.parse(data as string) as T)
}).catch((e: BusinessError) => {
logger.debug(TAG, "use kv store get value error", e.message)
if (e.code === 15100004) {
resolve(undefined)
} else {
reject(e)
}
})
})
})
}
set<T>(key: string, data: T): Promise<void> {
logger.debug(TAG, "use kv store set value", key, JSON.stringify(data))
return new Promise((resolve, reject) => {
this.connectStore().then(store => {
const json = JSON.stringify(data);
store.put(key, json).then(() => {
logger.debug(TAG, "use kv store set value success")
resolve()
}).catch((e: BusinessError) => {
logger.debug(TAG, "use kv store set value error", e.message)
reject(e)
})
})
})
}
remove(key: string): Promise<void> {
logger.debug(TAG, "use kv store remove value", key)
return new Promise((resolve, reject) => {
this.connectStore().then(store => store.delete(key).then(() => {
logger.debug(TAG, "use kv store remove value success")
resolve()
})).catch((e: BusinessError) => {
logger.debug(TAG, "use kv store remove value error")
reject(e)
})
})
}
}
重新封装后解决问题
登录 后才可以发表评论