I've got a `Flow` which I'm running `collectLatest()` on, but it's only firing the first time I send...
r
I've got a
Flow
which I'm running
collectLatest()
on, but it's only firing the first time I send something into it, not the second time... what can be the reasons here?
t
Do you have a code example you can provide
r
Trying to get some logs, but adb keeps SIGABRTing on me
t
A low tech way to debug is to just place .onEach{println()} at different places in the flow
r
My favourite way. println debugging. Wait, gotta refactor some code.
This is the consuming part:
Copy code
val scope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)

        val readerFlow = merge(disconnectedFlow, acsBluetoothReaderManager.onReaderDetection())

        scope.launch {
            readerFlow.onEach {
                Log.d("BTM", "Got a new reader to collect: $it")
            }
                .collectLatest { reader ->
The producing part:
Copy code
private val disconnectedFlow: MutableStateFlow<BluetoothReaderSupport?> = MutableStateFlow(null)
    private fun disconnectedCallback() {
        Log.d("BTM", "Disconnected Callback")
        val scope = CoroutineScope(Job())
        scope.launch {
            disconnectedFlow.emit(null)
        }
    }
The other producing part:
Copy code
fun BluetoothReaderManager.onReaderDetection(): Flow<BluetoothReaderSupport?> {
    val brm = this
    return callbackFlow<BluetoothReaderSupport?> {
        brm.setOnReaderDetectionListener { reader ->
            trySendBlocking(BluetoothReaderSupport(reader))
                .onSuccess { Log.d("BluetoothSupport", "Forwarded new reader: $reader") }
                .onFailure { exception -> throw exception!! }
        }
        awaitClose { brm.setOnReaderDetectionListener(null) }
    }
}
```
I've got a
D/BTM: Disconnected Callback
log line, but nothing afterwards.
Oh duh,
StateFlow
vs.
SharedFlow
. Changed it to:
Copy code
private val disconnectedFlow = MutableSharedFlow<BluetoothReaderSupport?>()
    private fun disconnectedCallback() {
        Log.d("BTM", "Disconnected Callback")
        if (disconnectedFlow.tryEmit(null)) {
            Log.d("BTM", "Emitted empty reader")
        } else {
            Log.w("BTM", "couldn't emit empty reader")
        }
    }
But the
tryEmit
gives me a
false
🤔
l
@reactormonk set the extraBufferCapacity to 1 in the
MutableSharedFlow