Skip to content

鸿蒙协程Dispatchers.Main没有初始化无法使用 #1276

@LiPengfei0106

Description

@LiPengfei0106

期望结果

鸿蒙平台有些方法需要切回主线程执行:

[default] [CheckThread] Fatal: ecma_vm cannot run in multi-thread! thread:27380 currentThread:27586

而鸿蒙平台上使用协程时需要切换到主线程时使用withContext(Dispatchers.Main)会报错:

kotlin.UninitializedPropertyAccessException: lateinit property tsfn has not been initialized

源码中好像没有任何地方会调用这个初始化方法来初始化tsfn

@OptIn(ExperimentalForeignApi::class)
public fun initMainHandler(env: napi_env) {
    tsfn = createTSFWithCallback(
        env,
        "MainHandlerWorker",
        staticCFunction(::callJSInThreadSafeFunction)
    )!!
    OH_LOG_Print(LOG_APP, LOG_INFO, 0u, "MainHandler", "initMainHandler success")
}

或者有其他方法来切换到主线程吗?

实际结果

重现链接

重现步骤

重现环境

补充说明

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    s: need triageRequires review and categorization.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions