taskpool使用异常
EntryAbility的onCreate方法中初始化了:
let appCtx = this.context.getApplicationContext()
let mmkvRootDir = MMKV.initialize(appCtx)
在taskpool里面调用接口,接口会获取MMKV里面的基地址,结果报错: getEnumNumberByResourceId error, code: undefined, message: getContext is not defined [(native_module_manager.cpp:594)(LoadNativeModule)] key is default/mmkv Error: You should Call MMKV.initialize() first. at defaultMMKV (entry|@tencent/mmkv|1.3.7|src/main/ets/utils/MMKV.ts:90:19) at getBaseUrl (entry|entry|1.0.0|src/main/ets/utils/ComConfig.ts:36:26) at func_main_0 (entry|entry|1.0.0|src/main/ets/utils/AxInstance.ts:13:14) updateTalkMsg error:ReferenceError: httpService is not initialized
Either that is running before EntryAbility onCreate(), or it's in a separate process.
You should be able to verify which one is the case by setting break points.
Either that is running before EntryAbility
onCreate(), or it's in a separate process. You should be able to verify which one is the case by setting break points.
@Concurrent
async function apiChatRenew(relationship: string): Promise
// 更新讨论区会话 export async function apiChatRenewTask(relationship: string) { let task: taskpool.Task = new taskpool.Task(apiChatRenew, relationship); taskpool.execute(task).then((result: Object) => { taskpool.cancel(task) }).catch((err: string) => { }); } 新建taskpool里面操作的,应该不算是不同进程
You should be able to investigate this by yourself.
You should be able to investigate this by yourself.
你的意思是现在处于不同进程,需要我自己断点定位解决是吗?
Either that is running before EntryAbility
onCreate(), or it's in a separate process. You should be able to verify which one is the case by setting break points.
Either xxx or xxx means two possibilities.
Setting breakpoints on your so-called taskpool code to verify which one is the case.
Closed due to inactive.
@shalang312119 你后面有排查吗? 我这边看taskpool是在相同进程,但是就是一直报错 You should Call MMKV.initialize() first
demo: https://github.com/wangqi060934/MmkvTest
@lingol I have the same problem. demo: demo: https://github.com/wangqi060934/MmkvTest 。 I'm sure MMKV.initialize() is called before taskpool , the ui thread and the taskpool thread is also in the same process
Hey guys, I found out the cause. Looks like taskpool has a separate set of global variables. The rootDir that the UI thread has initialized is null in the taskpool thread.
The workaround for this is the NameSpace feature introduced in v2.1.0.
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
mmkvRoot: string = '';
// save the root dir of the NameSpace
aboutToAppear(): void {
this.mmkvRoot = MMKV.rootDir; // or whatever root dir you like
let ns = MMKV.nameSpace(this.mmkvRoot);
let kv = ns.mmkvWithID('custom_id');
kv.encodeString("test", "111");
}
......
// pass the root dir into the taskpool
.onClick(() => {
this.message = 'Welcome';
taskpool.execute(mmkvTest, this.mmkvRoot);
})
......
// and using the root dir passed in
@Concurrent
function mmkvTest(namespaceRoot: string) {
try {
let ns = MMKV.nameSpace(namespaceRoot);
let kv = ns.mmkvWithID('custom_id');
const test = kv.decodeString("test", "222")
hilog.info(0x0001, 'mmkvTest', `get test: ${test}`);
} catch (e) {
hilog.error(0x0001, 'mmkvTest', e.message)
}
}
The complete demo is in the attachment.