Hey. I want to call a ViewModel function only ONCE...
# compose
j
Hey. I want to call a ViewModel function only ONCE in a composable. e.g. viewModel.getMovies() It should not be called on re-composition. How do I achieve this?
m
You can do this in the
init { }
block in the ViewModel itself
j
What if I cannot do it there? I share the ViewModel in multiple composables.
m
So to my understanding you are using different instances of ViewModel in different Composables, if so then the moviesList (which you are maintaining) will also be empty right
k
What about:
Copy code
LaunchedEffect(viewModel) {
    viewModel.getMovies()
}
j
LaunchedEffect works great, thanks
511 Views