Tips, Best Practices, and Real-world Insights
summary
Saving to Room via suspend DAO fails because viewModelScope gets canceled when the screen closes, so writes never finish; use an independent repository scope for persistence while keeping viewModelScope for reads. Name coroutines (example: CoroutineName for debugger filtering like "database updator" vs "remote service updator") to search StandaloneCoroutine objects in the heap and inspect context elements. Add a custom CoroutineContext trace ID to correlate logs across activity → viewmodel → repository. Never use runBlocking on main (or limited thread pools); it deadlocks—prefer reactive patterns, e.g. flows, for config loading.
AI-written summary · about
chapters (14)
- 0:00Coroutines introduction
- 1:05Coroutine scope challenges
- 6:09ViewModel scope cancels writes
- 9:46Independent write scopes
- 10:10Coroutine context elements
- 12:19Debugging with names
- 14:30Finding coroutines in memory
- 17:43Naming coroutines
- 18:27Custom logging context
- 25:46Underused coroutine context
- 26:07Dangers of runBlocking
- 27:16runBlocking deadlocks
- 30:30runBlocking alternatives
- 33:04Summary
related talks
28mMy coroutines patterns and habits, 3 years later
31mRoom and Workers. Spoiler alert: With Coroutines
37mCoroutines! Gotta catch 'em all!
35mKotlin Coroutine Mechanisms
49mLaunching Into Coroutines
39mKotlin Coroutine Mechanisms
28mConcurrency for Imposters
40mConcurrency made easy with Kotlin Coroutines
