Colton Idle
05/20/2023, 2:04 PMsuspend fun doSomeWork() {
coroutineScope.launch { suspendFunciton1() }
coroutineScope.launch { suspendFunciton2() }
}
Can anyone confirm that I'm correct about these statements (or am I somehow reading my test results wrong)
1. If I start doSomeWork
with viewModelScope
(for example), then suspendFunction1()
and 2()
will both be started with that same viewModelScope
to initiate doSomeWork
2. suspendFunction1()
and 2()
will run in parrallel
3. doSomeWork
won't complete until suspendFunction1()
and 2()
completeDominaezzz
05/20/2023, 2:10 PMephemient
05/20/2023, 2:10 PMuses Dispatchers.Main.immediate
which is a single thread
3. no, using another scope breaks the connection with the current oneDominaezzz
05/20/2023, 2:10 PMColton Idle
05/20/2023, 2:13 PMephemient
05/20/2023, 2:14 PMColton Idle
05/20/2023, 2:14 PMsuspend fun doSomeWork() {
viewModelScope.launch { suspendFunciton1() }
viewModelScope.launch { suspendFunciton2() }
}
Dominaezzz
05/20/2023, 2:15 PMcoroutineScope { launch {...}; launch {...}}
Colton Idle
05/20/2023, 2:19 PMsuspend fun doSomeWork() {
coroutineScope { launch { suspendFunciton1() } }
coroutineScope { launch { suspendFunciton2() } }
}
Can anyone confirm that I'm correct about these statements (or am I somehow reading my test results wrong)
1. If I start doSomeWork
with viewModelScope
(for example), then suspendFunction1()
and 2()
will both be started with that same viewModelScope
to initiate doSomeWork
2. suspendFunction1()
and 2()
will run in parrallel
3. doSomeWork
won't complete until suspendFunction1()
and 2()
completeephemient
05/20/2023, 2:21 PMDominaezzz
05/20/2023, 2:21 PMephemient
05/20/2023, 2:22 PMsuspend fun doSomeWork() {
suspendFunction1()
suspendFunction2()
}
just with some extra machineryColton Idle
05/20/2023, 2:52 PMephemient
05/20/2023, 2:53 PMColton Idle
05/20/2023, 2:55 PMcoroutineScope { launch {...}; launch {...}}
?Patrick Steiger
05/20/2023, 2:57 PMephemient
05/20/2023, 2:58 PMPatrick Steiger
05/20/2023, 2:58 PMcoroutineScope {}
, supervisorScope
or withContext
.Colton Idle
05/20/2023, 2:59 PMsuspend fun doSomeWork() {
coroutineScope {
launch { suspendFunciton1() }
launch { suspendFunciton2() }
}
}
Patrick Steiger
05/20/2023, 3:01 PMColton Idle
05/20/2023, 3:01 PMsuspend fun doSomeWork() {
viewModelScope.launch {
launch { suspendFunciton1() }
launch { suspendFunciton2() }
}
}
ephemient
05/20/2023, 3:02 PMsuspend fun foo() = coroutineScope {
since it's a relatively common pattern to want a scope with the same "lifetime" as the function callPatrick Steiger
05/20/2023, 3:03 PMephemient
05/20/2023, 3:03 PMPatrick Steiger
05/20/2023, 3:03 PMcoroutineScope
is wrong because it talks about parallel decomposition of workColton Idle
05/20/2023, 4:03 PMsuspend fun doSomeWork() {
coroutineScope {
launch { suspendFunciton1() }
launch { suspendFunciton2() }
}
}
the two inner suspend functions will be cancelled if the outer syspend function is cancelled right?
in other words. if i initially called doSomeWork with viewModelScope then the two inner suspend are called with that same "scope", no?Patrick Steiger
05/20/2023, 4:30 PMcoroutineScope
, the job of this scope is a child of the job of the enclosing suspend funcoroutineScope{}
jobColton Idle
05/20/2023, 4:40 PMsuspend fun doSomeWork() {
coroutineScope {
launch { suspendFunciton1() }
launch { suspendFunciton2() }
}
}
why wouldn't this be in parallel
suspend fun doSomeWork() {
coroutineScope {
launch { suspendFunciton1() }
}
coroutineScope {
launch { suspendFunciton2() }
}
}
Patrick Steiger
05/20/2023, 4:43 PMColton Idle
05/20/2023, 7:06 PMsuspend fun doSomeWork() {
coroutineScope {
launch { suspendFunciton1() }
launch { suspendFunciton2() }
}
}
how would I now be able to use their returns, once both of them return.
This works for me, but curiuos if theres some other standard way.
suspend fun doSomeWork(): Pair<MyType, OtherType> {
var one: MyType? = null
var two: OtherType? = null
coroutineScope {
launch { one = suspendFunciton1() }
launch { two = suspendFunciton2() }
}
return Pair(one, two)
}
Patrick Steiger
05/20/2023, 7:08 PMsuspend fun doSomeWork() =
coroutineScope {
val one = async { susFun1() }
val two = async { susFun2() }
one.await() to two.await()
}
Dominaezzz
05/20/2023, 7:09 PMsuspend fun doSomeWork(): Pair<MyType, OtherType> {
coroutineScope {
val one = async { suspendFunciton1() }
val two = async { suspendFunciton2() }
Pair(one.await(), two.await())
}
}
Colton Idle
05/20/2023, 11:07 PMephemient
05/20/2023, 11:18 PMval first: Type1
val second: Type2
coroutineScope {
val firstAsync = async { suspendFunction1() }
val second Async = async { suspendFunction2() }
first = firstAsync.await()
second = secondAsync.await()
}
// first and second are now initialized
works. destructuring an intermediate data structure is easier if an appropriate one exists, but this is an alternative if you have many different types