https://kotlinlang.org logo
Title
v

viralshah

01/24/2020, 6:09 PM
is there a good course or tutorial for Coroutines & Flows, I am a bit confused how and when to create which scopes
p

Pablichjenkov

01/24/2020, 6:17 PM
The scope is the same one.
v

viralshah

01/24/2020, 6:18 PM
Oh no I just meant in general, how and when to create scopes
Like when should I use RunBlocking vs creating a Coroutine Scope, and how do nested scopes relate to one another
p

Pablichjenkov

01/24/2020, 6:36 PM
Are you on Android or other platform? In the case of Android, if you are using the Androidx set of libraries, you get some scopes for granted. Anything that has a lifecycle has a lifecyclescope. In this case Activity, Fragment. ViewModels also have one. You can create one in your Application class. There is also GlobalScope from coroutine lib itself but is not encouraged to use it. Ideally you should create a scope out of a component that has "lifecycle". Then you create the scope when this component is created and cancel the scope when the component is destroyed.
v

viralshah

01/24/2020, 6:37 PM
Pure JVM, mainly SpringBoot apps
p

Pablichjenkov

01/24/2020, 6:41 PM
I see, I am really not familiar with SpringBoot framework. But in general the "lifecycle" concept applies. I guess you can have a CoroutineScope that has the scope of the SpringApplication. You could also create CoroutineScopes that has the lifecycle of a RequestController. You would get a better answer on the server channel or SpringBoot dedicated channel.
v

viralshah

01/24/2020, 6:42 PM
I see, will use that as a starting point, really appreciate the insight
👍 1
e

Evan R.

01/27/2020, 1:17 PM
I answered a similar question on coroutinescopes on reddit. Check my answer if you’re interested: https://www.reddit.com/r/Kotlin/comments/9x705v/how_to_use_coroutine_scope/e9rg7sh?utm_source=share&utm_medium=web2x
Only change I would make in my original answer is I would use
SupervisorJob()
instead of
Job()
so coroutines launched in the service’s scope can fail independently of one another
v

viralshah

01/27/2020, 6:32 PM
Thanks Evan, will for sure take a look