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

Kris Wong

06/09/2020, 8:19 PM
I am using
runBlocking(someContext)
, and within the block a call is made to a method that is calling
launch
using a scope that was created with the same context. however, it is not blocking. am I doing something wrong?
o

octylFractal

06/09/2020, 8:20 PM
could you share the code in full, or at least condensed? that sounds correct but maybe there's a miscommunication
s

streetsofboston

06/09/2020, 8:23 PM
Structured Concurrency only works within one scope. If a
launch
is launched with a different scope, it will not participate in the structured concurrency of the calling scope. It looks like sharing a CoroutineContext does not circumvent this...
k

Kris Wong

06/09/2020, 8:23 PM
Copy code
runBlocking(anObject.coroutineContext) {
    anObject.doSomething()
}
and
Copy code
fun doSomething() {
    coroutineScope.launch {
        ...
    }
}
finally
Copy code
internal val coroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private val coroutineScope = CoroutineScope(coroutineContext + SupervisorJob())
a scope just contains a context, so my understanding was that this is the way to accomplish what I am after
o

octylFractal

06/09/2020, 8:26 PM
no, this will not work because
doSomething
is not using a child job of
runBlocking
you should pass down the scope given in the
runBlocking
lambda, and use that to launch the appropriate job, OR if you mean to make it separate, return the Job from
doSomething()
and
join()
on it in
runBlocking
k

Kris Wong

06/09/2020, 8:27 PM
hmm
i am just trying to test something that is otherwise fully encapsulated, in the least invasive way
testing classes with no output always ends up being a pain =/
alright, at least I understand the problem, thanks!
8 Views