Ferran
11/13/2020, 11:29 AMSharedFlow
is available from org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1
? if not, which dependency shall I add for the commonMain
sourceset?launchIn
when invoked from iOS?ribesg
11/13/2020, 12:55 PMFerran
11/13/2020, 12:57 PMrunBlocking
but that doesn’t work. I’ve tried a custom implementation that doesn’t work either:
internal actual fun CustomMainScope(): CoroutineScope = CustomMainScopeImpl()
internal class CustomMainScopeImpl : CoroutineScope {
private val dispatcher = MainDispatcher()
private val job = Job()
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
println("${throwable.message}: ${throwable.cause}")
}
override val coroutineContext: CoroutineContext
get() = dispatcher + job + exceptionHandler
}
private class MainDispatcher : CoroutineDispatcher() {
@Suppress("TooGenericExceptionCaught")
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_main_queue()) {
try {
block.run()
} catch (err: Throwable) {
throw err
}
}
}
}
Maybe I’m missing something very obvious here :Sribesg
11/13/2020, 1:01 PMMainScope()
to run it on the UI threadFerran
11/13/2020, 1:02 PMribesg
11/13/2020, 1:03 PMCoroutineScope(newSingleThreadContext("Test"))
CoroutineExceptionHandler
to catch all uncaught exceptions in coroutines and deal with them properly@Suppress("FunctionName")
fun ErrorLoggingMainScope(log: Log, ignoreCancellationException: Boolean = true): CoroutineScope =
MainScope() + CoroutineExceptionHandler { _, e ->
if (ignoreCancellationException && e is CancellationException) return@CoroutineExceptionHandler
log.error("Uncaught error in coroutine", e)
}
Log
being my own thing which logs (lol) and also sends errors to BugsnagFerran
11/13/2020, 1:04 PMnewSingleThreadContextis not part of coroutines 1.4.1, is part of the
-native-mt
and as far as I know the mt branch version does not support StateFlow yetribesg
11/13/2020, 1:05 PMFerran
11/13/2020, 1:05 PMribesg
11/13/2020, 1:06 PMFerran
11/13/2020, 1:07 PMribesg
11/13/2020, 1:07 PMlouiscad
11/13/2020, 1:10 PMDispatchers.Main
gets working on iOS (or there's a newer native-mt
version, in the meantine): https://github.com/Kotlin/kotlinx.coroutines/issues/470#issuecomment-440080970Ferran
11/13/2020, 1:12 PM