Norbi
04/28/2021, 11:53 AM// This is my asynchronous/suspending input stream
interface AsyncInputStream {
// This method has semantics similar to InputStream.read() but suspends instead of blocking
suspend fun read(): Int
}
fun AsyncInputStream.asInputStream() = object : java.io.InputStream() {
override fun read(): Int {
return runBlocking { this@asInputStream.read() } // Is runBlocking() correct here?
}
}
If I understand correctly, using runBlocking
is incorrect because it "should not be used from a coroutine", and AsyncInputStream.asInputStream()
would possibly be called from coroutines.
Thanks.Zach Klippenstein (he/him) [MOD]
04/28/2021, 2:19 PMasInputStream
doesn’t call runBlocking
though, read
does.uli
04/28/2021, 2:23 PMNorbi
04/28/2021, 2:59 PMYes... although this is a general problem: in case of a non-suspend function how could I know that it won't be "used from a coroutine"?doesn’t callasInputStream
thoughrunBlocking
Zach Klippenstein (he/him) [MOD]
04/28/2021, 3:06 PM