Reputation: 39519
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 ;-)
Upvotes: 262
Views: 105188
Reputation: 4956
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.
Update2: After upgrading to Android Studio Flamingo(AGP 8), some warnings disappear; for those who don't, wrapping code in a withContext(Dispatchers.IO)
sometimes remove the warning. A related explanation: https://youtu.be/zluKcazgkV4?t=2400
Upvotes: 12
Reputation: 7226
A solution is to wrap blocking code via suspend fun kotlinx.coroutines.runInterruptible
.
Without it, the blocking code won't be interrupted, when coroutine's job gets cancelled.
It suppressed compile warning and blocking code will throw InterruptedException
on cancellation
val job = launch {
runInterruptible(Dispatchers.IO) {
Thread.sleep(500) // example blocking code
}
}
job.cancelAndJoin() // Cause will be 'java.lang.InterruptedException'
How to convert Java blocking function into cancellable suspend function?
Upvotes: 2
Reputation: 65
I ran into the same issue today, here goes the solution worked for me. Hope it helps!
CoroutineScope(Dispatchers.IO).launch {
val call = client.newCall(request)
call.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
print("Internet access, 4G, Wifi, DNS, etc failed")
}
override fun onResponse(call: Call, response: Response) {
if(response.isSuccessful) {
print("Server accepted!")
} else {
print("Server failed!")
}
}
})
}
Keep in mind this callbacks are consumed only once. You can't use it into other threads.
Upvotes: -2
Reputation: 5354
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)
}
}
}
Edit: The intended way to do it is to use witchContext(Dispatchers.IO)
. I leave this response here in case someone finds this approach useful.
Upvotes: 0
Reputation: 3359
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")
}
Upvotes: 2
Reputation: 8671
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()
}
}
Upvotes: -7
Reputation: 3059
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.
Upvotes: 112
Reputation: 1602
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))
}
}
Upvotes: 87
Reputation: 5093
If you do choose to suppress like some of the answers suggest, use
@Suppress("BlockingMethodInNonBlockingContext")
Upvotes: 20
Reputation: 7718
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.
Upvotes: 71
Reputation: 337
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.
Upvotes: 31