https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

molikuner

08/14/2019, 6:49 PM
Does this make any sense, or has any benefit?
Copy code
fun complete(): X {
    TODO("your implementation")
}
suspend fun suspendComplete(): X {
    return suspendCoroutine { continuation  ->
        val result = runCatching { complete() }
        continuation.resumeWith(result)
    }
}
🚫 7
l

louiscad

08/14/2019, 7:50 PM
suspendCancellableCoroutine
and its non cancellable counterpart are for callbacks which is not the case here. If your code is blocking, you'll want to use
withContext
and pass
Dispatchers.Default
for CPU bound operations, and
<http://Dispatchers.IO|Dispatchers.IO>
for blocking input/output. You should probably read the docs to see what's possible with kotlinx.coroutines.
If you want, I can also link one or more video talks that cover it.
m

molikuner

08/14/2019, 8:35 PM
Actually I found that code in a library and wasn't sure, so I asked. Now I'll comment there and probably link to this thread. So thank you for confirming my initial thought.
👍 1
l

louiscad

08/14/2019, 11:53 PM
Yes, please link that library so I can avoid it 😁
4 Views