efemoney
05/29/2019, 10:24 AMcoroutineScope
and launch
? (Its not super clear from the docs)gildor
05/29/2019, 10:24 AMefemoney
05/29/2019, 10:25 AMcoroutineScope
(I’m not entirely sure what it does)thana
05/29/2019, 10:26 AMgildor
05/29/2019, 10:26 AMcoroutineScope
Creates a CoroutineScope and calls the specified suspend block with this scope
This function is designed for parallel decomposition of work. When any child coroutine in this scope fails, this scope fails and all the rest of the children are cancelled (for a different behavior see supervisorScope). This function returns as soon as the given block and all its children coroutines are completed. A usage example of a scope looks like this:
thana
05/29/2019, 10:27 AMefemoney
05/29/2019, 10:27 AMCoroutineScope
, I understand that. Sorry I wasnt clear, I meant the builder coroutineScope { ... }
coroutineScope
suspend the gildor
05/29/2019, 10:39 AMsuspend the threadThis statement is incorrect by definition, you cannot suspend thread
efemoney
05/29/2019, 10:40 AMgildor
05/29/2019, 10:41 AMefemoney
05/29/2019, 10:41 AMgildor
05/29/2019, 10:41 AMdesigned for parallel decompositionIt means that you do something in parallel inside of this block
efemoney
05/29/2019, 10:45 AMlaunch
is what I need (for a side-effecty kind of thing).gildor
05/29/2019, 10:45 AMcoroutineScope { launch { doSomething() } }
doesn’t make sensecoroutineScope {
launch { doSomething() }
doSomethingElse()
}
efemoney
05/29/2019, 10:46 AMlaunch { ... }
instead of a coroutineScope { ... }
in the outer coroutine (which is a launch btw).
It’s all cleared up now really.gildor
05/29/2019, 10:48 AMefemoney
05/29/2019, 10:55 AMdescription
and startedAt
) and I have to start a timer with the value of startedAt and set text with the value of description. So:
launch {
val data = getData() // suspend fun
runTimer(data.startedAt)
settext(data.description)
}
// ... elsewhere
fun runTimer(startedAt) = launch {// or coroutineScope
// ... do timer things with ticker()
}
My confusion was whether to wrap runTimer body in coroutineScope or not, of course that wouldnt have worked because runTimer just runs a ticker that counts up forever, suspending the parent coroutine and therefore never getting to setText()gildor
05/29/2019, 10:57 AMfun runTimer(startedAt) = launchThis is not recommended style, coroutineScope is recommended, but it has very different sematics
efemoney
05/29/2019, 11:01 AMrunTimer
but it shouldn’t suspend the parent coroutine because I still want to reach setText
.
Also, both `launch`es are called in a larger CoroutineScope
(my UI component) so the launch
is not necessarily just hanging…gildor
05/29/2019, 11:02 AMbut it shouldn’t suspend the parent coroutine because I still want to reachRecommended approach is: launch { runTimer(data.startedAt) } suspend fun runTimer(startedAt).setText
efemoney
05/29/2019, 11:03 AMdepends on runTimer implementation and how it should workThink of it as a loop with
delay(1000)
and textView.text = now - startedAt
gildor
05/29/2019, 11:04 AMfun CoroutineScope.runTimer(…): Job {
return launch { }
}
efemoney
05/29/2019, 11:05 AMRecommended approach is:I see. Why is this so much different from wrapping the body in launch. The method is private so it wont be called from anywhere else.
gildor
05/29/2019, 11:06 AMefemoney
05/29/2019, 11:06 AMgildor
05/29/2019, 11:06 AMefemoney
05/29/2019, 11:07 AMgildor
05/29/2019, 11:12 AM