How to make "inappropriate blocking method call" appropriate?
KotlinOkhttpKotlin CoroutinesMoshiKotlin Problem Overview
I am currently trying to leverage kotlin coroutines more. But I face a problem: when using moshi or okhttp inside these coroutines I get a warning:
"inappropriate blocking method call"
What is the best way to fix these? I really do not want to be inappropriate ;-)
Kotlin Solutions
Solution 1 - Kotlin
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.
Solution 2 - Kotlin
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.
Solution 3 - Kotlin
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))
}
}
Solution 4 - Kotlin
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.
Solution 5 - Kotlin
If you do choose to suppress like some of the answers suggest, use
@Suppress("BlockingMethodInNonBlockingContext")
Solution 6 - Kotlin
I'm using Android Studio 4.1, and the warning shows when I use Moshi
or manipulate File
. Wrapping the code in a withContext
doesn't help even if I'm sure about what I'm doing.
I recently found out that moving the tiny code that warns into a standard method without suspend
like fun action() {...}
can remove the warning. This is ugly since it simply hides the warning.
Update: From my personal experience, it appears suppressing the warning or runBlocking is more straightforward.
Solution 7 - Kotlin
It looks like encasing the call in kotlin.runCatching()
resolves the warning, not sure why though...
val result = kotlin.runCatching {
OldJavaLib.blockingCallThatThrowsAnException()
}
if (result.isSuccess) {
print("success is on your side")
} else {
print("one failure is never the end")
}
Solution 8 - Kotlin
You get this warning because Coroutines should never block, but suspend instead.
This is how you suspend your Coroutine, run your blocking method in a thread, and resume it on result. This will also handle exceptions, so your app won't crash.
suspendCoroutine { continuation ->
thread {
try {
doHttpRequest(URL(...)) {
continuation.resume(it)
}
}
catch (t: Throwable) {
continuation.resumeWithException(t)
}
}
}
Solution 9 - Kotlin
I used dispatchers as launch arguments:
GlobalScope.launch(Dispatchers.IO) {
// Do background work
// Back to main thread
launch(Dispatchers.Main) {
Toast.makeText(context, "SUCCESS!", Toast.LENGTH_LONG)
.show()
}
}