Ferran
11/10/2020, 9:19 AM1.4.0
we can use SharedFlow
. Is this supported from kotlin multiplatform native? in this example:
fun start() {
actionsFlow.asSharedFlow().onEach { action ->
when (action) {
Action.Refresh -> refresh()
Action.Load -> Unit
}
}.launchIn(coroutineScope)
}
suspend fun action(action: Action) {
actionsFlow.emit(action)
}
it works from Android but from iOS there are no events coming through the flow, the coroutineScope implementation (CustomMainScope) that I’m using for iOS is the following:
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
}
}
}
}
and the dependency for iOS is:
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0"
Is SharedFlow
supposed to work on iOS even if its on a main thread?ruwinmike
11/10/2020, 9:31 AMSharedFlow
fix https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.4.1Ferran
11/10/2020, 9:32 AMJohn O'Reilly
11/10/2020, 9:44 AM-native-mt
version of kotlinx coroutines.....unfortunately a 1.4.x version of that is not available yet (see https://kotlinlang.slack.com/archives/C1CFAFJSK/p1603745028163400)ruwinmike
11/10/2020, 9:58 AMFerran
11/10/2020, 10:09 AMprivate val _sharedFlow = MutableSharedFlow<String>()
fun start(callback: (String) -> Unit) {
runBlocking {
callback("call from blocking")
withContext(Dispatchers.Default) {
callback("call from with context")
}
_sharedFlow.asSharedFlow()
.onEach { text -> callback(text) }
.launchIn(this)
}
}
fun action(action: ContentUseCase.Action) = runBlocking {
_sharedFlow.emit("value from flow")
}
I can see the two call from…
but I don’t see the value from flow
I’m not sure I can get it more basic than that :Spablisco
11/11/2020, 10:36 AMFerran
11/11/2020, 2:53 PMrunBlocking
is not a good solution for Android because it blocks the main thread. I’m only using that for having a scope from iOS, otherwise I’m not sure what to use. Open to suggestions 🙂pablisco
11/11/2020, 3:54 PM