Consider following function
suspend fun writeToStream(osw: OutputStreamWriter, text: String) = withContext(Dispatchers.IO) {
osw.use{it.write(text)} //AndroidStudio warns about 'write' - Inappropiate blocking method call
}
Does the warning mean that the function is not actually suspending?
I've looked into this thread, and based on the comment by Tenfour4, it seems that the warning may be incorrect (apparently there are some false positives warnings):
A blocking call is still a blocking call but Dispatchers.IO can tolerate blocking calls because unlike Dispatchers.Default it can create as many threads as it needs. Other dispatchers could run out of threads if they all get locked up by blocking calls
this code doesn't show warning, but is obviously not suspending:
fun writeToStream(osw: OutputStreamWriter, text: String) = runBlocking(Dispatchers.IO) {
osw.use { it.write(text) }
}
To sum up - does the blocking call nullify the benefits of suspend? Is it possible to have suspend function that uses blocking call, and has all the benefits of suspend? Is the AndroidStudio's warning incorrect, as it's not aware that I use Dispatchers.IO
?