zoha131
02/12/2021, 1:04 AMDisposableEffect
with empty onDispose
is a code smell. And we should avoid that.
I want to load an article from server after user open the detail page. What should be the recommended way to load the data?
Previously I was using onActive
to load the data. ViewModel emits StateFlow
which the view collect as state.
// pre- Alpha-11
onActive { viewModel.loadData(articleID) }
// post - Alpha-11
DisposableEffect(articleID){
viewModel.loadData(articleID)
onDispose { }
}
Zach Klippenstein (he/him) [MOD]
02/12/2021, 1:32 AMzoha131
02/12/2021, 1:50 AMallan.conda
02/12/2021, 3:19 AMallan.conda
02/12/2021, 3:20 AMzoha131
02/12/2021, 3:44 AMDominaezzz
02/12/2021, 7:13 AMallan.conda
02/12/2021, 7:14 AMMarko Novakovic
02/12/2021, 10:11 AMSideEffect {}
for running code when composable is composed/recomposed. it may not be the best idea to call viewModel.loadData()
from there but you can use SideEffect
for some other things. DisposableEffect
is used when you have resources to clear aka dispose. DisposableEffect
with empty onDispose() {}
is just a SideEffect
zoha131
02/12/2021, 10:40 AMonResume
then I moved from fragment to composables for screens and was using onActive
to load data for that screen using ViewModel. And any ongoing work will be stopped if the ViewModel gets cleared.
Now that onActive
is gone I have to use DisposableEffect
but having empty onDispose
in almost all the screens feel wired and also a code smell.
Have I done anything wrong? or I need to think differently? I am thinking compose as a screen not a view.allan.conda
02/12/2021, 10:45 AMzoha131
02/12/2021, 10:52 AMallan.conda
02/12/2021, 10:53 AMzoha131
02/12/2021, 10:55 AMallan.conda
02/12/2021, 10:56 AMzoha131
02/12/2021, 11:01 AMallan.conda
02/12/2021, 11:04 AMMarko Novakovic
02/12/2021, 12:07 PMSideEffect
is DisposableEffect
with no onDispose
so when you need something like DisposableEffect
and you don’t have use for onDispose
just use SideEffect
Marko Novakovic
02/12/2021, 12:08 PMSideEffect
is executed on every composition and recompositionMarko Novakovic
02/12/2021, 12:08 PMViewModel
with factory and pass id into it via factory, that is the best approachMarko Novakovic
02/12/2021, 12:10 PMViewModel
just like Fragment
of Activity
so when you call viewModel()
you will get ViewModel
that is tied to a composable. that allows you to call code related to a composable inside ViewModel.init
block and be sure it’s cleared if composable is removed from the treezoha131
02/12/2021, 12:12 PMAssistedInject
as described by @allan.conda. Thank you all for helping me with this. 😇Marko Novakovic
02/12/2021, 12:13 PMSideEffect
and DisposableEffect
for future use. these two are important and have their uses. good luckZach Klippenstein (he/him) [MOD]
02/12/2021, 3:29 PMCyril Find
02/15/2021, 9:00 AMLaunchedEffect(articleID) { ... }
here ?zoha131
02/16/2021, 4:47 AMLaunchedEffect(articleID) { ... }
and DisposableEffect(articleID) { … }
with empty onDispose
both works here. But these are not recommended. At least this is what I understood after reading all the messages here.Cyril Find
02/16/2021, 8:58 AMLaunchedEffect(articleID) { ... }
is not recommended ?
I'm kinda confused by the exchange too 😅 that's why I was asking thisDominaezzz
02/16/2021, 9:06 AM