I usually trigger this type of resource loading right at the init block of the ViewModel, but if you want more control you could also do it when your @Composable first creates using the onActive { chatVm.loadMessages() } (which the docs say it is practically just a onCommit(true) { chatVm.loadMessages() })
Jeisson Sáchica
11/20/2020, 10:56 PM
If you are using navigation you could also do it when your screen is created:
Copy code
NavHost(navController = navController, startDestination = "chat") {
// Other routes
composable("chat") {
val chatVm: ChatViewModel by viewModels()
chatVm.loadMessages()
val msgs by chatVm.messages.observeAsState()
ChatScreen(messages = msgs)
}
}