https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
f

Ferran

11/10/2020, 9:19 AM
with Kotlin
1.4.0
we can use
SharedFlow
. Is this supported from kotlin multiplatform native? in this example:
Copy code
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:
Copy code
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:
Copy code
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0"
Is
SharedFlow
supposed to work on iOS even if its on a main thread?
r

ruwinmike

11/10/2020, 9:31 AM
I see new version of coroutines with
SharedFlow
fix https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.4.1
f

Ferran

11/10/2020, 9:32 AM
👍 I’ll give it a go thanks
j

John O'Reilly

11/10/2020, 9:44 AM
depending on your setup you may also need to use
-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)
r

ruwinmike

11/10/2020, 9:58 AM
but version with native-mt doesn’t contain features from 1.4.+
f

Ferran

11/10/2020, 10:09 AM
@ruwinmike updating to 1.4.1 didn’t work
@John O'Reilly I’m just checking the basics without multithreading, I assumed that sharedflow and stateflow should just work out of the box even for iOS on a single thread…
cc @pablisco
this is the code
Copy code
private 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 :S
p

pablisco

11/11/2020, 10:36 AM
And that code works fine on JVM right? Just to make sure 🙂
f

Ferran

11/11/2020, 2:53 PM
@pablisco the
runBlocking
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 🙂
p

pablisco

11/11/2020, 3:54 PM
Hmm, yeah Android seems to have it’s own artifact for dispatcher: https://github.com/Kotlin/kotlinx.coroutines/tree/master/ui I guess we don’t have one for iOS 🤔
2 Views