juliocbcotta
06/16/2022, 1:59 PMmyScope (supervisor + Main + interceptor)
myScope.launch {
text = mySusFunc()
}
suspend fun mySusFunc(): String {
return withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
delay(1000)
"myValue"
}
}
Would the attribution text =
always happen in the Main ? Because from some experimenting here, it looks like it happens from the IO thread. Help ?Joffrey
06/16/2022, 2:01 PMMain
, if myScope
uses Main
as dispatcher. But if other pieces of the context in myScope
override the dispatcher, it would be that dispatcher instead.Sam
06/16/2022, 2:01 PMinterceptor
?juliocbcotta
06/16/2022, 2:06 PMjuliocbcotta
06/16/2022, 2:07 PMSam
06/16/2022, 2:07 PMjuliocbcotta
06/16/2022, 2:08 PMSam
06/16/2022, 2:09 PMjuliocbcotta
06/16/2022, 2:10 PMSam
06/16/2022, 2:12 PMinterceptContinuation
, instead of just returning the continuation, pass it to another interceptor first (which will be the dispatcher)
e.g.
private class MyInterceptor(
private val dispatcher: CoroutineDispatcher,
) : ContinuationInterceptor {
override val key: CoroutineContext.Key<*>
get() = ContinuationInterceptor
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
// do your interceptor stuff here
return dispatcher.interceptContinuation(continuation);
}
}
juliocbcotta
06/16/2022, 2:13 PMSam
06/16/2022, 2:15 PMjuliocbcotta
06/16/2022, 2:15 PMSam
06/16/2022, 2:17 PMSupervisorJob() +
Dispatchers.Main.immediate +
viewScopeInterceptor(Dispatchers.Main.immediate)
to just
SupervisorJob() + viewScopeInterceptor(Dispatchers.Main.immediate)
because the view scope interceptor will replace the dispatcher as soon as you add it (they have the same key in the coroutine context)juliocbcotta
06/16/2022, 2:18 PM+
would be cumulative and not replace things...Sam
06/16/2022, 2:19 PMjuliocbcotta
06/16/2022, 2:21 PMContinuationInterceptor
and It is just that when I read ContinuationInterceptor
I think that I can have a chain of it...Sam
06/16/2022, 2:25 PMContinuationInterceptor
😞 (e.g. in your code):
override val key: CoroutineContext.Key<*>
get() = ContinuationInterceptor
Sam
06/16/2022, 2:25 PMjuliocbcotta
06/16/2022, 2:27 PMSam
06/16/2022, 2:32 PMCoroutineDispatcher
, it has the same key as well.juliocbcotta
06/16/2022, 2:34 PM