Sean McQuillan
May 25, 2019 · 1 min read

This is definitely something that is being looked into (see: https://github.com/Kotlin/kotlinx.coroutines/issues/242)

It turns out it’s actually a fairly complicated problem — what exactly does it mean for a CoroutineScope (or Dispatcher) to be idle? To understand a bit of the problem: it’s OK to write an infinite loop like this in a coroutine to create a timer:

val secondsSinceStarted = liveData {
var seconds = 0
while(true) {
emit(seconds++)
}
}

This will cleanup correctly in viewModelScope, but it will never be fully “idle.”

There’s quite a few ideas on the issue I linked above (and some working code). Definitely take a look!

Sean McQuillan

Written by

Android Developer Advocate @Google

More From Medium

More from Sean McQuillan

More from Sean McQuillan

Top on Medium

Ed Yong
Mar 25 · 22 min read

8.7K

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade