I'm wondering how to structure the code inside the class where I need something like an interval from RxJava, that'll do heavy operation every 50ms. The problem is that I'd want the channel producer to execute heavy operation only when someone is collecting the flow. I don't want to waste resources. Is there some easy way to achieve it?
class Foo {
val flow: Flow<Unit> = flow {
while (true) {
// TODO run heavyOperation only when someones collection flow
heavyOperation()
emit(Unit) // or any result of operation
delay(50)
}
}
}
gildor
07/07/2020, 3:16 AM
If you want only 1 heavyOperation even with multiple observers, you have to wait for SharedFlow