如何将挂起函数作为参数传递给另一个函数? Kotlin 协程
how to pass suspend function as parameter to another function? Kotlin Coroutines
我想将挂起函数作为参数发送,但它显示“修饰符 'suspend' 不适用于‘值参数’”。怎么做?
fun MyModel.onBG(suspend bar: () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
Lambda 的 suspend
修饰符应该放在冒号字符之后,而不是前面。示例:
fun MyModel.onBG(bar: suspend () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
我想将挂起函数作为参数发送,但它显示“修饰符 'suspend' 不适用于‘值参数’”。怎么做?
fun MyModel.onBG(suspend bar: () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
Lambda 的 suspend
修饰符应该放在冒号字符之后,而不是前面。示例:
fun MyModel.onBG(bar: suspend () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}