Mark Vogel
01/17/2023, 12:26 AMwithContext(<http://Dispacters.IO|Dispacters.IO>)
(for I/O functions).
For example, more specifically, I'm attempting to convert the fetch()
from jOOQ to a suspending function:
suspend fun <R : Record> ResultQuery<R>.await(): Result<R> =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { fetch() }
Is this the correct way of going about this?ephemient
01/17/2023, 12:49 AMResultQuery<R>
into a Flow<R>
Mark Vogel
01/17/2023, 12:52 AMwithContext(Dispatcher)
under the hood?ephemient
01/17/2023, 12:56 AM.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
that wayephemient
01/17/2023, 12:57 AMMark Vogel
01/17/2023, 1:05 AMephemient
01/17/2023, 1:07 AMephemient
01/17/2023, 1:09 AM.asFlow()
, Kotlin suspends (allowing the underlying thread to handle other coroutines) until upstream publishes another recordephemient
01/17/2023, 1:11 AMMark Vogel
01/17/2023, 1:11 AMMark Vogel
01/17/2023, 1:12 AMephemient
01/17/2023, 1:13 AMsuspend fun
. if it returns multiple results then a Flow<*>
is better (and functions returning Flow
should generally not be suspend
themselves)ephemient
01/17/2023, 1:17 AMsuspendCancellableCoroutine
or callbackFlow
to adapt it. if it's using Java8 futures, Java9 publishers, Reactive Streams, RxJava, etc… then there's already adapters that existMark Vogel
01/17/2023, 1:18 AMwithContext
to block a thread and suspend until the blocking function returns?ephemient
01/17/2023, 1:19 AMMark Vogel
01/17/2023, 1:26 AMephemient
01/17/2023, 1:27 AMwithContext
does suspend on the original dispatcher, but then the coroutine on the IO
dispatcher is blocking, not suspendingMark Vogel
01/17/2023, 1:39 AMMark Vogel
01/17/2023, 1:39 AMgildor
01/17/2023, 6:43 AMephemient
01/17/2023, 8:08 AM