Harun
11/25/2018, 9:19 AMlouiscad
11/25/2018, 2:11 PMimport kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
open class SuspendLazy<out T>(
private val dispatcher: CoroutineDispatcher = Dispatchers.Default,
initializer: () -> T
) {
private val lazyValue = lazy(initializer)
suspend operator fun invoke(): T = with(lazyValue) {
if (isInitialized()) value else withContext(dispatcher) { value }
}
}
Tolriq
11/25/2018, 2:59 PMAlexey Pushkarev
11/25/2018, 9:01 PMasad.awadia
11/26/2018, 2:33 AMwithContext
?Paul Woitaschek
11/26/2018, 9:30 AMmersan
11/26/2018, 10:16 AMSam
11/26/2018, 2:34 PMOrfeo Ciano
11/26/2018, 3:26 PMasad.awadia
11/26/2018, 3:56 PMsuspendCoroutine
to wrap an async callback into a suspend function - which dispatcher does it use to run on? The same dispatcher that called it?Sam
11/26/2018, 7:58 PMclass LambdaClass : suspend () -> Unit {
override fun invoke() {
// do something
}
}
mersan
11/26/2018, 10:47 PMasad.awadia
11/27/2018, 12:49 AMawait
for completion of a suspending function that wraps a blocking call with withContext
Andrew Gazelka
11/27/2018, 1:52 PMoshai
11/27/2018, 2:00 PMjava.lang.NoClassDefFoundError: kotlinx/coroutines/SupervisorJobImpl
Sam
11/27/2018, 10:49 PMfun main( args : Array<String> ) {
val handler = CoroutineExceptionHandler { _, throwable -> println( "Handled $throwable" ) }
runBlocking(handler) {
launch {
println( "Handler is ${coroutineContext[CoroutineExceptionHandler]}" )
delay( 1000)
println( "first task" )
throw UnsupportedOperationException()
}
}
}
spand
11/28/2018, 8:19 AMJob()
as an explicit lifecycle as mentioned here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#cancellation-via-explicit-job
Is installing a CoroutineExceptionHandler
the only way to handle exceptions in the lifecycle ?Tolriq
11/28/2018, 11:16 AMPavel.AZ
11/28/2018, 11:29 AMbloder
11/28/2018, 12:19 PMDiefferson
11/28/2018, 1:12 PMbdawg.io
11/29/2018, 6:47 AMTolriq
11/29/2018, 9:05 AMmingkangpan
11/29/2018, 11:08 AMlaunch {
val isYes = channel.receive
if(!isYes) {
coroutineContext.cancel()
return@launch
}
//process further logic
}
gotoOla
11/29/2018, 1:30 PMmyanmarking
11/29/2018, 7:14 PMdewildte
11/30/2018, 5:06 AMJoey Heck
11/30/2018, 8:03 PMval job = GlobalScope.launch {
<How do I access the Job from here>
}
Tolriq
11/30/2018, 8:07 PMsuspend fun doLoad() = suspendCancellableCoroutine<Result> {
val cancellationSignal = CancellationSignal()
it.invokeOnCancellation { cancellationSignal.cancel() }
val result = query.cancellationSignal(cancellationSignal).executeAsync()
if (cancellationSignal.isCanceled) {
it.resume(Result.Cancelled)
} else {
it.resume(Result.Success(result))
}
}
In that example all works if executeAsync is blocking, but if executeAsync is a suspend function this no more works. Since cancellation rely on the cancellation signal how to merge the 2 worlds?v79
12/01/2018, 8:12 AMsuspend
functions p(), q(),..., and I want them to run in parallel/concurrently. Is this a valid approach?