Colton Idle
06/15/2024, 12:32 PMLauncedEffect(myViewModel)?
Something else?Ian Lake
06/15/2024, 2:29 PMDispatchers.Main.immediate
is actually the real problem child, since it will start (and potentially complete work) before the creation of the object completes and the composition it is associated with is actually valid. Using any other dispatcher that always dispatches removes that particular problemIan Lake
06/15/2024, 2:30 PMMarko Novaković
06/15/2024, 3:58 PMstateIn
with WhileSubscribed
.
if I need to do something else I create my own fun init() {…}
that I explicitly call from a composable using:
rememberSaveable { viewModel.init(); 1 }
I don’t know if there is a better way to do this but this worksStylianos Gakis
06/15/2024, 4:18 PMinit
you gotta make sure that you do in fact want it to only be done on init. More or less every time I've seen that pattern that was not the case, but they did want that work to be done when the screen came back (from popping other screens in the backstack) into view and the VM at that point was already there, it didn't just initialize.
So cold flows that rely on a listener being there + collectAsStateWithLifefycle is more or less always what you wanna do instead, exactly as Ian explained.Colton Idle
06/17/2024, 4:43 PMMore or less every time I've seen that pattern that was not the case, but they did want that work to be done when the screen came back (from popping other screens in the backstack)interesting. in my case I feel like i end up seeing this a lot in the opposite direction. i want the work only done once, and i definitely do not want the work to be done again because i came back to the screen. great points all around though. Maybe it I truly want something done once, I should just put it in the rememberSaveable (even though that kinda feels ugly and feels like my LaunchedEffect trick too) lol
Stylianos Gakis
06/17/2024, 5:26 PMColton Idle
06/17/2024, 5:35 PMIan Lake
06/17/2024, 5:44 PM