manueldidonna
06/09/2020, 3:40 PMlaunch
a coroutine, delay
it and then mutate the state. How can I correctly handle the lifecycle of the composition?Leland Richardson [G]
06/09/2020, 3:46 PMlaunchInComposition
it will all be handled for youmanueldidonna
06/09/2020, 3:58 PMlaunchInComposition
before posting here. Maybe I didn't understand how the lifecycle of a composable behaves. If I update a state, I trigger a recomposition? And the coroutine launched with launchInComposition
will stay alive?Zach Klippenstein (he/him) [MOD]
06/09/2020, 4:07 PMIf I update a state, I trigger a recomposition?Depends what you mean by “state”. Updating a
MutableState
value (i.e. what’s returned by state { }
and savedInstanceState { }
) will trigger a recomposition. This is how collectAsState
works, for example.
And the coroutine launched withThe coroutine launched by this function will stay active as long as thewill stay alive?launchInComposition
launchInComposition
remains in the composition. If you change your state in such a way that, on the next composition, you no longer call launchInComposition
in the same position (e.g. it’s surrounded by an if
that changes from true to false), then the coroutine will be cancelled. The function also optionally takes one or more keys, which behave like keys to state
or remember
– if those keys change between compositions, the coroutine will be cancelled and a new one started. More info from Adam here: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1591659323449600?thread_ts=1591653158.447300&cid=CJLTWPH7Smanueldidonna
06/09/2020, 4:11 PMlaunchInComposition
worksVinay Gaba
06/09/2020, 4:12 PMlaunchInComposition
taking care of switching threads for me?Zach Klippenstein (he/him) [MOD]
06/09/2020, 4:16 PMonCommit
-type functions (see that thread i linked above for more on this too). E.g. One of the compose devs mentioned one use case was pre-rendering AdapterList
items in the background before they’re needed on-screen.Zach Klippenstein (he/him) [MOD]
06/09/2020, 4:17 PMlaunchInComposition
launches coroutines on the Dispatchers.Main
dispatcher, and otherwise has the same semantics around threading that the standard coroutine launch
does. If you want to switch threads inside of that you can use withContext
or any of the other coroutine facilities for managing context.Zach Klippenstein (he/him) [MOD]
06/09/2020, 4:19 PMMutableState.value
from different threads in FrameManager.framed { }
still, or you’ll get an exception about not being in a frame. Apparently there will eventually be some global frame management magic that will make this unnecessary, but last time I checked it was still required.Vinay Gaba
06/09/2020, 4:23 PMFrameManager.framed { }
recently so I exactly know which error you are talking about 😄Leland Richardson [G]
06/09/2020, 5:18 PMLeland Richardson [G]
06/09/2020, 5:20 PMFrameManager.framed { … }
won’t be needed on background threads. We are also investigating how we can make this seamlessly interleave with launchInComposition
in pretty interesting ways, but we don’t have anything solid there yet.Vinay Gaba
06/09/2020, 6:08 PMAdam Powell
06/09/2020, 6:26 PMAdam Powell
06/09/2020, 6:27 PMZach Klippenstein (he/him) [MOD]
06/11/2020, 12:32 AM