A deep dive on the lifecycle-aware coroutines APIs
summary
Shows when UI should stop collecting (maps location example) and which lifecycle APIs actually stop work: repeatOnLifecycle, flowWithLifecycle, collectAsStateWithLifecycle. Dissects implementations: repeatOnLifecycle uses suspendCancellableCoroutine, flowWithLifecycle is built on callbackFlow, collectAsStateWithLifecycle wraps produceState; warns cold flows backed by Channel/buffers or hidden buffering (flowOn) can keep producers active and waste resources. Notes KMP compatibility caveat and an API regret: the atRepeatingJob helper breaks structured concurrency and was discarded.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 5:00Why lifecycle collection matters
- 7:23Manual collection pitfalls
- 8:05APIs overview
- 9:09How repeatOnLifecycle works
- 15:13Implementation details
- 21:35Using callbackFlow
- 24:02How collectAsState works
- 26:29Designing good APIs
- 27:31Pitfalls of atRepeatingJob
- 28:54Key takeaways
related talks
07mCollecting flows in a lifecycle-aware manner
36mAsynchrony and the infinite conveyor belt: Advanced Coroutines and Flows
28mMy coroutines patterns and habits, 3 years later
36mLifecycle for Android Developers
44mGoing with the Flow - Using Coroutines 1.3.x
40mDeep diving into the Kotlin Coroutines API
45mDeep Dive into Coroutines on JVM
38mAndroid Coroutines: How to manage async tasks in Kotlin
