I've got a rather callback-heavy API, which I've m...
# coroutines
r
I've got a rather callback-heavy API, which I've modeled roughly as
Copy code
fun onReaderConnected(): Flow<Reader> { ... }
class Reader {
  fun onCard(): Flow<Card>
}
And I'm consuming it via
Copy code
onReaderConnected() { reader ->
  reader.onCard().collect {
    ... process card ...
  }
}
Now I've run into the issue where the reader disconnects, the inner
collect
won't stop, so the new reader connection won't get activated. How do I best model that?
s
Why are you not using
flatMap
in this case? ­čĄö
Copy code
val x: Flow<Unit> = onReaderConnected().flatMapConcat {
  reader.onCard().map {
    /** process card */
  }
}
Or
flatMapLatest
based on desired behavior
Then you can use
collect()
or
launchIn
at the edge.
r
flatMapLatest
looks exactly what I'm looking for, thanks! Now to figure out why it doesn't behave as it should ­čĄö
l
collectLatest might also do the trick depending on whether you need the end result in a Flow.