I'm wondering how to structure the code inside the...
# flow
m
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?
g
Use flow, do not use channel
Copy code
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)
        }
    }
}
If you want only 1 heavyOperation even with multiple observers, you have to wait for SharedFlow