Ferran

    Ferran

    1 year ago
    with Kotlin
    1.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?
    r

    ruwinmike

    1 year ago
    I see new version of coroutines with
    SharedFlow
    fix https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.4.1
    Ferran

    Ferran

    1 year ago
    👍 I’ll give it a go thanks
    John O'Reilly

    John O'Reilly

    1 year ago
    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

    1 year ago
    but version with native-mt doesn’t contain features from 1.4.+
    Ferran

    Ferran

    1 year ago
    @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
    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
    pablisco

    pablisco

    1 year ago
    And that code works fine on JVM right? Just to make sure 🙂
    Ferran

    Ferran

    1 year ago
    @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 🙂
    pablisco

    pablisco

    1 year ago
    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 🤔