spierce7
12/20/2018, 5:19 AMgildor
12/20/2018, 5:26 AMspierce7
12/20/2018, 5:40 AMis
checks to pull data, or is there another way (I see there is a CoroutineContext.Key)? What about the same information being available in the child coroutine contexts as well?gildor
12/20/2018, 6:46 AMand usingNo, you can just use Key for that and when you get request by Key it will be already casted to your context classchecks to pull datais
What about the same information being available in the child coroutine contexts as well?The same as now, you can pass parent context explicitly, most of primitives just getting parent context and merge with some own, like
CoroutineScope
context will be used for all child coroutines or withContext
merges passed context with parent and so onval coroutineName = coroutineContext[CoroutineName]?.name
dave08
12/20/2018, 9:46 AMgildor
12/20/2018, 9:49 AMwhich as far as I understand is just for tracking coroutine statethis is not true, coroutineContext is for any coroutine related metadata
dave08
12/20/2018, 9:50 AMgildor
12/20/2018, 10:03 AMdave08
12/20/2018, 12:17 PMspierce7
12/20/2018, 2:28 PMCombinedContext
. Would I need to override some additional methods?
@Test fun coroutineTest() = runBlocking {
TestScope.launch {
assertEquals(CoroutineName::class, coroutineContext::class)
}.join()
}
object TestScope : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = CoroutineName("Blah")
}
data class CoroutineName(
val name: String
) : AbstractCoroutineContextElement(CoroutineName) {
companion object Key : CoroutineContext.Key<CoroutineName>
override fun toString(): String = "CoroutineName($name)"
}
gildor
12/20/2018, 2:59 PMcoroutineContext
is not only your item, it’s merged with other default contexts, just add println(coroutineContext)
in launch
to check what is there, most obvious thing is Job, because you use launchspierce7
12/20/2018, 4:09 PMcompanion object Key : CoroutineContext.Key<CoroutineName>
, it stops working. Is reflection being used to retrieve the key for the context? How does that work?gildor
12/20/2018, 4:24 PMspierce7
12/20/2018, 6:02 PMcompanion object Key : CoroutineContext.Key<CoroutineName>
from the CoroutineName
class it no longer worksgildor
12/21/2018, 2:14 AMIf I removeOf course, because companion object used as a Keyfrom thecompanion object Key : CoroutineContext.Key<CoroutineName>
class it no longer worksCoroutineName
coroutineContext[CoroutineName]
It’s actually the same as
coroutineContext[CoroutineName.Key]
Just implicit reference to companion objectspierce7
12/21/2018, 2:28 AMgildor
12/21/2018, 2:28 AMspierce7
12/21/2018, 2:28 AMgildor
12/21/2018, 2:28 AMspierce7
12/21/2018, 2:28 AMgildor
12/21/2018, 2:29 AMclass Foo
Foo
, there is no such syntax in Kotlin, correct?
Like
val foo = Foo
spierce7
12/21/2018, 2:30 AMgildor
12/21/2018, 2:30 AMclass Foo {
companion object
}
val foo = Foo
foo
contains instance of companion object of Foospierce7
12/21/2018, 2:31 AMgildor
12/21/2018, 2:32 AMKey
is redundand, it’s just a conventionspierce7
12/21/2018, 2:32 AMgildor
12/21/2018, 2:32 AMcompanion object : CoroutineContext.Key<CoroutineName>
spierce7
12/21/2018, 2:33 AMgildor
12/21/2018, 2:33 AMspierce7
12/21/2018, 2:34 AMCoroutineName is CoroutineContext.Key
gildor
12/21/2018, 2:35 AMCoroutineName
is not a type, but companion object instancespierce7
12/21/2018, 2:35 AMcoroutineContext
, how do I access it's companion object?gildor
12/21/2018, 2:36 AMcoroutineContext[SomeCoroutineContextKey]
spierce7
12/21/2018, 2:37 AMSomeCoroutineContextKey
has to be stored inside of the coroutineContext
right?gildor
12/21/2018, 2:40 AMAbstractCoroutineContextElement
, or just implement Elementspierce7
12/21/2018, 2:42 AMgildor
12/21/2018, 2:42 AMkey
property with keyspierce7
12/21/2018, 2:43 AMpublic abstract class AbstractCoroutineContextElement(public override val key: Key<*>) : Element
AbstractCoroutineContextElement
gildor
12/21/2018, 2:44 AMspierce7
12/21/2018, 2:44 AMgildor
12/21/2018, 2:47 AM