CoroutineScope.coroutineContext deprecated, how do I do it now?


#1

With coroutines library 0.24.0 I get this warning:
image

My code is like this:

job = launch(UI){
   async(coroutineContext+CommonPool){
   }
}

I want to launch sub-job inside of launch{}. This was child of parent job when coroutineContext was used as parameter. It’s deprecated, and I see only option to pass kotlin.coroutines.experimental.coroutineContext as parameter, which is long and ugly. Is there some other way, or how is this meant to work now?


#2

Which coroutineContext are you currently importing if not the kotlin.coroutines.experimental.coroutineContext one?


#3

I don’t import any, it uses those from kotlinx.coroutines.experimental.CoroutineScope.coroutineContext, and CoroutineScope is receiver parameter in coroutine block run by launch.

So these 2 have same name, and it automatically picks CoroutineScope.coroutineContext.


#4

Are you by any chance using kotlin 1.3-M1? Because they are moving coroutiens out of the experimental package. There are some steps that need to be done for migration and calling code still compiled with the old experimental coroutines.
I can’t really tell you more because I did not yet try out the preview build of 1.3 myself. More info is here though:


#5
buildscript{
    ext.kotlin_version = '1.2.60'
...

#6

Hi,
You have to import kotlin.coroutines.experimental.* or kotlin.coroutines.experimental.coroutineContext.
CoroutineScope.coroutineContext is now marked as LowPriorityInOverloadResolution and deprecated with replacement, so IDEA can even quickfix that for you: https://screencast.com/t/ogvQkmSu7uOo


#7

Thanks, yes quickfix was able to fix this (add the import), so now it’s clear and solved.