'How to make "inappropriate blocking method call" appropriate?
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 ;-)
Solution 1:[1]
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:[2]
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:[3]
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:[4]
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:[5]
If you do choose to suppress like some of the answers suggest, use
@Suppress("BlockingMethodInNonBlockingContext")
Solution 6:[6]
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:[7]
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 8:[8]
It looks like encasing the call in kotlin.runCatching()
resolves the warning, not sure why though... Because as previous answer about runCatching states it's not due to the exception throwing since even try{} catch doesn't resolve the issue, could be some buggy detection issue...
I ended up using the below for now...
val result = kotlin.runCatching {
OldJavaLib.blockingCallThatThrowsAnException()
}
if (result.isSuccess) {
print("success is on your side")
} else {
print("one failure is never the end")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | theapache64 |
Solution 2 | |
Solution 3 | Syed Umair |
Solution 4 | |
Solution 5 | seekingStillness |
Solution 6 | |
Solution 7 | |
Solution 8 |