Kotlin Coroutines 101 - Android Conference Talks

Android Developers · 148K views · 2020 · 24:49

Open on YouTube

Programming language designSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it as a solid starting point on Kotlin coroutines, including how dispatchers decide which thread code runs on and how coroutines are scoped in Android.

Recommended by 2 people across 1 different months, 2019 to 2021.

What it is

A 25-minute Android conference talk in which Manuel Vivo, a Google Developer Relations engineer, introduces Kotlin Coroutines. It covers the problems coroutines solve, how to use them in Android apps, and how to test them.

What people said

Strong rec MrVasilev · 2020-04 · Stack Overflow · on “What scope should I use when calling a suspended function from a Java Activity?”

Here [LINK] you can find some useful info about coroutines and also here [this video] is one of the best video for coroutines.

Recommends Animesh Sahu · 2020-04 · Stack Overflow · on “Do suspend functions execute in another thread and come back with the result?”

TLDR; There is no guarantee, it may or may not, it really depends that on the following points: Is the dispatcher is multi-threaded? Is there any dispatcher override in between? LONG Answer A coroutine has a CoroutineContext that specify how it behaves, where it run. A CoroutineContext is mainly build up with four elements: Job , CoroutineName , CoroutineExceptionHandler and Dispatcher . Its responsibility of the dispatcher to dispatch the coroutine. A dispatcher can be paused to stop coroutines to even run (this is useful in unit testing) mentioned here in the android conference talk [this video] , it may be a single-threaded dispatcher just like Dispatchers.Main , it has an event-loop like java

Cites Dewey Reed · 2019-02 · Stack Overflow · on “When to use Kotlin suspend keyword?”

fun startAsyncFunc() { launch { asyncFunc1() asyncFunc2() } } fun asyncFunc1() { ... } suspend fun asyncFunc2() { ... } I can finish the work without suspend and it even makes test easier (it can be tested without adding runBlocking . My questions: asyncFunc1 vs asyncFunc2 , which is better and why? If asyncFunc2 is better, should I always use suspend whenever a function will be ran in the coroutines? Update In the recent releases of Kotlin Coroutines, I notice if a method doesn't contain any coroutines code(like launch , async , etc), the compiler complains This inspection reports a suspend modifier as redundant if no other suspend functions are called inside . So I assume that suspend shou

Cites Sam Chen · 2021-08 · Stack Overflow · on “How to use coroutines in a Kotlin Library”

how do i correctly use coroutines in a Library class which has nothing to do with the activity lifecycle? Well, I would recommend you don't create coroutineScope inside your library class , instead you turn your class function into suspend function by specifying which thread (Dispatcher) it should run on: suspend fun doWork() { withContext(Dispatchers.IO) { //run on IO thread //do your stuff } } AND THEN, use built-in coroutineScope like viewModelScope from ViewModel , or lifecycleScope from Activity / Fragment to execute this suspend function. Those built-in coroutineScopes will be auto-canceld after the ViewModel or Activity / Fragment get destroyed, so you don't need to worry about them: