https://kotlinlang.org logo
Title
p

Paul Woitaschek

06/23/2021, 4:46 PM
I think some time ago I read an article that suggested to always use the main dispatcher by default and only switch to sth else on demand. Can anybody recall if he read that too and where?
w

wbertan

06/23/2021, 4:51 PM
I remember something on those lines 🤔 Could find this one: https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb
Well written suspend functions are always safe to call from the main thread (or main-safe).
It’s a really good idea to make every suspend function main-safe. If it does anything that touches the disk, network, or even just uses too much CPU, use 
withContext
 to make it safe to call from the main thread. This is the pattern that coroutines based libraries like Retrofit and Room follow. If you follow this style throughout your codebase your code will be much simpler and avoid mixing threading concerns with application logic. When followed consistently, coroutines are free to launch on the main thread and make network or database requests with simple code while guaranteeing users won’t see “jank.”
p

Paul Woitaschek

06/25/2021, 10:09 AM
@Matt Thompson Huh, exactly that one. Big thanks! ❤️