The following code shows “Inappropriate blocking m...
# android
s
The following code shows “Inappropriate blocking method call” warning. Can someone explain the reason for the warning and share some way to fix it?
Copy code
override suspend fun write(data: ByteArray) = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
    context.openFileOutput(fileName, Context.MODE_PRIVATE).use {
        it.write(data)
    }
}
c
openFileOutput
throws an
IOException
, and that warning expects it to be wrapped in a try/catch block, too
s
The warning is still there even if I wrapped it with catch block.
It’s gone when I wrapped the code block with
Copy code
runCatching {
}
I don’t know the difference.
Copy code
try {
    context.openFileOutput(fileName, Context.MODE_PRIVATE).use {
        it.write(data)
    }
} catch (e: IOException) {
}
vs
Copy code
runCatching {
    context.openFileOutput(fileName, Context.MODE_PRIVATE).use {
        it.write(data)
    }
}
g
There is no difference, it’s a bug of Kotlin Plugin check
Just ignore it
this check is created to detect blocking calls in suspend contexts, but your code is completely fine, you wrap it with IO dispatcher already, so it’s false positive
It’s gone when I wrapped the code block with
It’s one more sign that this check is not very accurate, runCatching just hides this call in lambda, so check doesn’t detect that runCatching is just an inline function with try/catch
s
Thanks for your answer, @gildor
👌 1
g
also found that check with launch(IO) works fine, but not withContext(IO) So I would report it to Kotlin IDE Plugin tracker
and that warning expects it to be wrapped in a try/catch block, too
I don’t think it’s correct, this warning is not expecting try/catch or any other error handling