Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.77k stars 1.82k forks source link

Kotlin collect flow wont work in a react-native bridge #4087

Closed prz3m01299 closed 1 month ago

prz3m01299 commented 1 month ago

I successfully created a bridge and I call the Nearby API by creating a controllerSessionScope, and I receive a log from that API. But then once I try to collect a prepared session flow inside a coroutine it won't run at all.

@ReactMethod
    fun controllerInitiateSessionTest(callback: Callback) {
        CoroutineScope(Dispatchers.IO).launch {
            val result = controllerInitiateRanging()
            callback.invoke(null, result)
        }
    }

Then the controllerInitiateRanging is called ->

suspend fun controllerInitiateRainging(callback: Callback) {

    [...]

    val controllerSession = uwbManager.controllerSessionScope() 
    //This returns the log "Creating Gms Client session scope"

    [...]

    val sessionFlow = controllerSession.prepareSession(partnerParameters)

    job = CoroutineScope(Dispatchers.IO).launch {
                Log.d("Coroutine", "Started coroutine with session flow")
                sessionFlow.collect {
                    print("Collecting session")
                    when (it) {
                        is RangingResult.RangingResultPosition -> handlePositionResult(it.position)
                        is RangingResult.RangingResultPeerDisconnected -> handlePeerDisconnect(it)
                    }
                }
            }
}

It stops executing after the sessionFlow.collect (the same code works in native Kotlin, outside of the bridge)

dkhalanskyjb commented 1 month ago

Sorry, but we (kotlinx.coroutines maintainers) don't know anything about "react native bridges." You'll probably have better luck asking this on Stack Overflow or the Kotlin Slack, where the community members may have knowledge of the topic. Maybe it's impossible to make it work, maybe you're missing some piece, maybe the framework you're using has an issue, or maybe our library is at fault. If it turns out that there is an issue in our library that we need to fix to make it work in your situation, please explain it to us, and we'll look into it.