Hello community, hope you all doing well. I am get...
# coroutines
h
Hello community, hope you all doing well. I am getting compilation error by this code. This code written in BaseFragment, and I am trying to use it in the child. The error I am getting is:
e: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call 'collectInMainBy' into
So why it can’t be inlined?
Copy code
inline fun <reified T> Flow<T>.collectInMainBy(
    crossinline onCollect: suspend (T) -> Unit,
    crossinline onFailure: suspend (Throwable) -> Unit = { Timber.e(it, tag) }
) {
    onEachCatching(onCollect, onFailure).launchInMain(lifecycleScope)
}

inline fun <reified T> Flow<T>.onEachCatching(
    crossinline onEach: suspend (T) -> Unit,
    crossinline onFailure: suspend (Throwable) -> Unit
) : Flow<T> {
    return onEach {
        onEach(it)
    }.catch {
        onFailure(it)
    }
}

fun <T> Flow<T>.launchInMain(scope: CoroutineScope): Job = scope.launch(Dispatchers.Main) {
    collect()
}
Call side:
Copy code
buttonProceed.clicks().collectInMainBy(
    onCollect = { viewModel.proceedButtonClicked() }
)
k
I think onFailure default block is not suspend.
1
Can u post your full error
h
I solved this problem by replacing
crossinline
to
noinline
. Dunno why with
crossinline
modificator the
onFailure
block default implementation couldn’t be inlined.