https://kotlinlang.org logo
#coroutines
Title
# coroutines
z

zak.taccardi

01/11/2021, 7:11 PM
I need a flow builder that emits
Unit
every
X
milliseconds while it has a subscriber. What’s the idiomatic approach for this?
s

streetsofboston

01/11/2021, 7:13 PM
Copy code
val flow = flow {
    while (isActive) {
        emit(Unit)
        delay(period)
    }
}
👍 2
^^^ This is a cold timer; each
flow
collector/subscriber (re-)starts the flow.
👍 2
z

zak.taccardi

01/11/2021, 7:16 PM
yeah, would need to add
.shareIn(..)
g

gildor

01/11/2021, 11:40 PM
Why do you need shareIn for this? It's indeed will work too, but looks that for this use case cold version also works just fine
2 Views