reactormonk
09/23/2022, 12:35 PMFlow
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?Trevor Stone
09/23/2022, 1:09 PMreactormonk
09/23/2022, 1:40 PMTrevor Stone
09/23/2022, 1:42 PMreactormonk
09/23/2022, 1:55 PMreactormonk
09/23/2022, 2:11 PMval 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:
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:
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) }
}
}
```reactormonk
09/23/2022, 2:12 PMD/BTM: Disconnected Callback
log line, but nothing afterwards.reactormonk
09/23/2022, 2:39 PMStateFlow
vs. SharedFlow
.
Changed it to:
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
🤔louiscad
09/23/2022, 3:13 PMMutableSharedFlow