我需要传递一个协程函数作为另一个函数的参数。例如:
private fun handleIt(here: Long, call: (hereId: Long) -> Unit) {
call(here)
}然后从协程作用域:
GlobalScope.launch {
handleIt(3) { viewModel.doThings(hereId) }
}viewModel函数如下所示:
suspend fun doThings(hereId: Long) {
withContext(coroutineContextProvider.io) {
doSomething(hereId)
}
}但是现在,我得到了一个错误:“挂起函数只能在协程主体中调用。有什么建议吗?”
发布于 2019-03-16 18:39:42
简单的解决方案是将块和handleIt函数都标记为挂起:
suspend fun handleIt(here: Long, call: suspend (hereId: Long) -> Unit) {
call(here)
}https://stackoverflow.com/questions/55195666
复制相似问题