https://kotlinlang.org logo
Title
l

Lilly

09/10/2021, 12:16 AM
[SOLVED] I have a
channelFlow
that stops sending to collector after some time and I can't figure out why:
override fun readByteArrayStream(): Flow<ByteArray> = channelFlow {
        try {
            requireNotNull(bluetoothSocket) { "Bluetooth socket is null. Connection has been closed." }

            val inputStream = bluetoothSocket!!.inputStream

            val buffer = ByteArray(1024)

            while (isActive) {
                val numBytes = inputStream.read(buffer)
                val readBytes = buffer.copyOf(numBytes)
                Log.d("Reading", "$readBytes")
                send(readBytes)
            }
        } catch (e: Exception) {
            Log.e("Error", "error: ${e.message}")
        }
    }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
It's still logging so the flow didn't stop. Any ideas?
r

radityagumay

09/10/2021, 12:24 AM
Have you check whether it's still active?
and shouldn't we add awaitClose()?
l

Lilly

09/10/2021, 12:29 AM
both tested, that's not the problem unfortunately
This also does not work, when I use
flow
builder
j

Johnjake Talledo

09/10/2021, 4:16 AM
probably because of this
val inputStream = bluetoothSocket!!.inputStream
having a null value
l

Lilly

09/10/2021, 8:33 AM
That's also not the problem. readBytes is logged and has data, it just don't send/emit it to the collector
I could figure it out. Some of the intermediate operators seemed to block the chain
r

radityagumay

09/28/2021, 12:02 AM
which one? @Lilly
l

Lilly

09/28/2021, 11:50 AM
@radityagumay It was a self made operator. I was playing around with timeouts but I discarded the idea
r

radityagumay

09/29/2021, 12:00 AM
ah sure