将Coroutine函数作为函数参数传递



我需要将coroutine函数作为另一个函数的参数传递。例如:

private fun handleIt(here: Long, call: (hereId: Long) -> Unit) {
            call(here)
}

,然后从Coroutine范围:

GlobalScope.launch {
                        handleIt(3) { viewModel.doThings(hereId) }
                    }

ViewModel函数看起来像这样:

suspend fun doThings(hereId: Long) {
        withContext(coroutineContextProvider.io) {
            doSomething(hereId)
        }
    }

但是现在,我发现了错误:"悬架功能只能在Coroutine主体中调用。任何建议?

简单的解决方案将标记块和 handleIt函数暂停:

suspend fun handleIt(here: Long, call: suspend (hereId: Long) -> Unit) {
    call(here)
}

最新更新