james
10/18/2021, 7:56 AMsomeParentId
), and that screen is made up of some child Composables, and those Composables each have their own ViewModels. I would like to re-initialize those child ViewModels when someParentId
changes, so that they re-load their data (and in turn recompose whatever is required)
if I completely blow away the child ViewModels when someParentId
changes and construct new ones, the corresponding UI must be recomposed every time, right? and that would be a very inefficient way to do it?
so does this mean that the correct way would be to have ParentViewModel hold a reference to ChildA ViewModel and ChildB ViewModel, and when someParentId
changes I can just get my existing child ViewModel references, and tell them to fetch the relevant new data?
(hastily drawn pic for reference in case the relationships were unclear)Daniel
10/18/2021, 8:18 AMsomeParentId
as the key to trigger data reload in the ChildVIewModels, when someParentId
changes..
LaunchedEffect(key = someParentId) {
childViewModel.reload()
}
This also means that you need to expose someParentId
as an observable type (flow, rx) and the observe it as compose state. Optimally pass it as a function paramter all the way down to the child composables.Daniel
10/18/2021, 8:20 AM