Archie
07/22/2020, 1:35 PMCoroutines
with compose?Adam Powell
07/22/2020, 1:40 PMlaunchInComposition
will launch a coroutine into a scope managed by the composition. If the call leaves the composition the scope is cancelled for you. https://developer.android.com/reference/kotlin/androidx/compose/package-summary#launchincompositionArchie
07/22/2020, 1:41 PMlaunchInComposition
, now I was wondering how I could run my coroutines without it getting cancelled by device rotation similar to launching coroutines in viewModelScope
. Is this possible?Adam Powell
07/22/2020, 4:23 PMviewModelScope
in the usual wayArchie
07/22/2020, 7:00 PMAdam Powell
07/22/2020, 8:18 PMby viewModels()
API for fragments/activities floating around but I'm not sure which things made it into the actual api just yet 🙂Timo Drick
07/22/2020, 11:27 PMAdam Powell
07/23/2020, 12:30 AMlaunchInComposition
making suspending calls to a ViewModel makes a lot of senseviewModelScope.async { ... }
internally, store the Deferred
, and return the .await()
of that operationTimo Drick
07/23/2020, 12:49 AMAdam Powell
07/23/2020, 1:09 AMviewModelScope
in my own code thanks to this 🤷♂️Archie
07/23/2020, 2:43 AMtbh I find very little use forI got confused with this. So do we needin my own code thanks to thisviewModelScope
viewModel
or not? 🤔Adam Powell
07/23/2020, 3:18 AMviewModelScope
CoroutineScope I don't tend to use very often. It depends on what you want to scope and whyArchie
07/23/2020, 1:35 PMFor the moment anyway. I think we had a couple of compose extensions that behave similarly to theI accidentally found this in compose:API for fragments/activities floating around but I'm not sure which things made it into the actual api just yet 🙂by viewModels()
val mainViewModel: MainViewModel = viewModel()
where viewModel()
is a @Composable
function.
(Thank for pointing out the existing api <3)
Is it right to assume that the ViewModel
created here is scope to the Activity
/`Fragment` Hosting the @Composable
function?