Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "inappropriate blocking method call" appropriate?

People also ask

How do I fix inappropriate blocked call?

Wrap the "inappropriate blocking method call" code in another context using withContext . This will suspend the current coroutine, then execute the "inappropriate blocking call" on a different thread (from either the Dispatchers.IO or Dispatchers.

What is runBlocking Kotlin?

The name of runBlocking means that the thread that runs it (in this case — the main thread) gets blocked for the duration of the call, until all the coroutines inside runBlocking { ... } complete their execution.

What is Dispatchers io?

IO Dispatcher:It starts the coroutine in the IO thread, it is used to perform all the data operations such as networking, reading, or writing from the database, reading, or writing to the files eg: Fetching data from the database is an IO operation, which is done on the IO thread.

What is a kotlin coroutine?

A coroutine is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages.


The warning is about methods that block current thread and coroutine cannot be properly suspended. This way, you lose all benefits of coroutines and downgrade to one job per thread again.

Each case should be handled in a different way. For suspendable http calls you can use ktor http client. But sometimes there is no library for your case, so you can either write your own solution or ignore this warning.

Edit: withContext(Dispatchers.IO) or some custom dispatcher can be used to workaround the problem. Thanks for the comments.


You also get this warning when calling a suspending function that is annotated with @Throws(IOException::class) (Kotlin 1.3.61). Not sure if that is intended or not. Anyway, you can suppress this warning by removing that annotation or changing it to Exception class.


Exceptions can occur that's why it shows this warning. Use runCatching{}. It catches any Throwable exception that was thrown from the block function execution and encapsulating it as a failure.

For Example:

 CoroutineScope(Dispatchers.IO).launch {
         runCatching{
               makeHttpRequest(URL(downloadLocation))
         }
}

Wrap the "inappropriate blocking method call" code in another context using withContext.

That is to say (for example):

If you are doing a read/write blocking method call:

val objects = withContext(Dispatchers.IO) { dao.getAll() }

If you are performing a blocking network request (using Retrofit):

val response = withContext(Dispatchers.IO) { call.execute() }

Or if you are performing a CPU intensive blocking task:

val sortedUsers = withContext(Dispatchers.Default) { users.sortByName() }

This will suspend the current coroutine, then execute the "inappropriate blocking call" on a different thread (from either the Dispatchers.IO or Dispatchers.Default pools), thereby not blocking the thread your coroutine is executing on.