问题
I want to send suspending function as a parameter, but it shows " Modifier 'suspend' is not applicable to 'value parameter" . how to do it?
fun MyModel.onBG(suspend bar: () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
回答1:
Lambda's suspend
modifier should be placed after the colon character, not in front. Example:
fun MyModel.onBG(bar: suspend () -> Unit) {
launch {
withContext(Dispatchers.IO) {
bar()
}
}
}
来源:https://stackoverflow.com/questions/55886676/how-to-pass-suspend-function-as-parameter-to-another-function-kotlin-coroutines