Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I tried to use Kotlin Coroutine Channels and got an ObsoleteCoroutinesApi warning. Where is the replacement?

I tried to use Kotlin's coroutine channels, but got a warning about the code using an ObsoleteCoroutinesApi. Where is the replacement for the deprecated channels code?

like image 839
colintheshots Avatar asked Nov 12 '18 21:11

colintheshots


People also ask

Can coroutine replace thread?

You can suspend execution and do work on other threads while using a different mechanism for scheduling and managing that work. However, this version of kotlinx. coroutines cannot change threads on its own.

What is ObsoleteCoroutinesApi?

common. annotation class ObsoleteCoroutinesApi. Content copied to clipboard. Marks declarations that are obsolete in coroutines API, which means that the design of the corresponding declarations has serious known flaws and they will be redesigned in the future.

Are Kotlin channels thread safe?

Channel class kotlinx. coroutines library is thread-safe. It is designed to support multiple threads.

How do I close a channel on Kotlin?

Closes this channel. This is an idempotent operation — subsequent invocations of this function have no effect and return false . Conceptually, it sends a special "close token" over this channel. Immediately after invocation of this function, isClosedForSend starts returning true .


1 Answers

As of today, no replacement yet exists for the Kotlin Coroutine Channels API. Despite confusing naming, they added this annotation to indicate that the existing API is being rewritten and will be replaced.

It's a warning that you can accept. If you have kotlinOptions.allWarningsAsErrors = true stopping you from building your app, you can simply add the @ObsoleteCoroutinesApi annotation to the top of the class to indicate that you accept the risk that your code will require changes.

However, this may quickly spiral out of control as you need to apply these markers to every class that uses these APIs and then every dependency that uses those classes, ad infinitum. To accept these risks project-wide, add the following to your gradle options:

    kotlinOptions.freeCompilerArgs += [
            "-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi",
            "-Xuse-experimental=kotlinx.coroutines.ObsoleteCoroutinesApi"]

Feel free to update this answer when a replacement API exists.

like image 191
colintheshots Avatar answered Nov 15 '22 09:11

colintheshots