https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

ken_kentan

06/09/2020, 1:34 AM
Hi, I got warning
Inappropriate blocking method call
when call blocking method in suspend function. I don't know that why this code was warned... How to fix this?
Copy code
import java.net.URL

suspend fun foo(bar: String): String {
    if (bar.isEmpty()) {
        return ""
    }
    return withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        // warning: Inappropriate blocking method call
        URL("<https://www.example.com>")
            .readText()
    }
}
o

octylFractal

06/09/2020, 1:59 AM
it seems fine, I believe this is an intellij bug but I can't find an exact issue that looks the same
1
you might want to file one yourself
Nope… no that one
Actually, cannot reproduce. Works fine for me.
If I remove
withContext
it complains on
URL
which is a bug: https://youtrack.jetbrains.com/issue/KT-39352
But it does not complain on `readText`: https://youtrack.jetbrains.com/issue/KT-39476
l

louiscad

06/09/2020, 7:49 AM
Where is that list of blocking methods?
e

elizarov

06/09/2020, 7:56 AM
There’s no like. It consider any Java method that
throws IOException
to be potentially blocking (which is quite a good heuristic for Java world)
👍 3
And it is not really a coroutine-inspection. It is quite complicated beast that understands “non blocking reactive contexts” and warn on potential blocking code usage in them
k

ken_kentan

06/09/2020, 8:41 AM
Thank you. I understood. I will ignore this warning using
@Suppress
annotation.
e

EyeCon

06/09/2020, 1:05 PM
I had the same issue using Jsoup's `get()`which I suppressed.
Copy code
val doc = Jsoup.connect(workUrl).get()
...triggers the same warning.
8 Views