MMKV icon indicating copy to clipboard operation
MMKV copied to clipboard

taskpool使用异常

Open shalang312119 opened this issue 1 year ago • 5 comments

image 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

shalang312119 avatar Aug 27 '24 09:08 shalang312119

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.

lingol avatar Aug 27 '24 12:08 lingol

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 { try { ChatApi.apiChatRenew(relationship) } catch (e) { HtLogUtil.error("updateTalkMsg error:" + e) } return 1; }

// 更新讨论区会话 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里面操作的,应该不算是不同进程

shalang312119 avatar Aug 28 '24 06:08 shalang312119

You should be able to investigate this by yourself.

lingol avatar Aug 28 '24 06:08 lingol

You should be able to investigate this by yourself.

你的意思是现在处于不同进程,需要我自己断点定位解决是吗?

shalang312119 avatar Aug 28 '24 07:08 shalang312119

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.

lingol avatar Aug 28 '24 08:08 lingol

Closed due to inactive.

lingol avatar Sep 13 '24 06:09 lingol

@shalang312119 你后面有排查吗? 我这边看taskpool是在相同进程,但是就是一直报错 You should Call MMKV.initialize() first

Image

demo: https://github.com/wangqi060934/MmkvTest

wangqi060934 avatar Apr 24 '25 14:04 wangqi060934

@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

wangqi060934 avatar Apr 24 '25 14:04 wangqi060934

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.

Index.ets.zip

lingol avatar Apr 25 '25 10:04 lingol