Juan Rada
10/25/2023, 2:00 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
if want to call function from a coroutine?kevin.cianfarini
10/25/2023, 2:02 PMwhile (true) { }
is also blocking.
Blocking in terms of needing to offload to a different thread really just means that something is a slow synchronous APIRiccardo Lippolis
10/25/2023, 2:03 PM@Blocking
annotation available in the JetBrains java-annotations library, which at least gives some IntelliJ IDE support (and possibilities for static analysis), maybe that helpsJuan Rada
10/25/2023, 2:05 PMAlexandru Nedelcu
10/25/2023, 2:24 PMAlexandru Nedelcu
10/25/2023, 2:26 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
in your implementation, instead of relying on the caller to do it.
I'd also add that it should probably be runInterruptible(<http://Dispatchers.IO|Dispatchers.IO>
.ephemient
10/25/2023, 2:31 PMIOException
in a suspending context that isn't wrapped in withContext(<http://Dispatchers.IO|Dispatchers.IO>)
. it's definitely not perfect (https://youtrack.jetbrains.com/issue/KTIJ-843) but it catches some common Java IO methodsJacob
10/25/2023, 2:36 PMfranztesca
10/25/2023, 2:47 PMobject Blocking
suspend fun <R> runSuspending(block: Blocking.() -> R): R =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { Blocking.block() }
context(Blocking)
fun myBlockingFunction() {
Thread.sleep(1000)
}
context(Blocking)
fun myOtherFunction() {
myBlockingFunction()
}
suspend fun main() {
// Doesn't compile
myOtherFunction()
runSuspending {
myOtherFunction()
}
}
Alexandru Nedelcu
10/25/2023, 3:22 PMAlexandru Nedelcu
10/25/2023, 3:24 PM