Hey all ! A question / discussion š
private var job: Job? = null
set(value) {
println("Setting value")
field = value
}
job = lifecycleScope.launch() {
println("Started")
try {
println("Try")
} catch (e: CancellationException) {
println("Catch")
} finally {
println("Finally")
}
}
prints
Started
Try
Catch
Finally
Setting value
in order.
Which means when using default coroutine start,
launch
returns
job
reference
after contents are executed ( if execution is fast enough ). So thereās no guarantee when youāll be handled
job
reference when you do the assignment. I didnāt find any mention of this in any docs, but I was expecting it to at least wait for reference to be handled before starting execution š¤
This is probably troublesome in scenarios if you do an
if ( job != null ) return
check or similar ( clear in
finally
(
finally { job = null }
). You may end up
job
being assigned and not getting cleared.
P.S. Using
CorotuineStart.LAZY
followed by
job?.start()
resolves this issue, Iām just asking if is this expected from default implementation š¤ ?