Nikola Milovic
04/09/2021, 9:01 AMfun A() {
B()
C()
}
fun B(){
scope.launch{
//call some suspending fun
}
}
So now C will be executed before B finishes even tho B isn't suspending fun?binding.exitButton.setOnClickListener {
autoSave()
viewModel.navigateToMainMenu()
}
Navigate is a regular function while
fun autoSave() {
uiScope.launch{
viewModel.autoSave()
}
}
viewModel.autosave is a suspending fun. Now I need it to first save and then proceed to navigate. As now it's not happening in that orderMarko Novakovic
04/09/2021, 9:08 AMuiScope.launch {}
in order to call viewModel.autoSave()
?ViewModel
and then you won’t have this kind of problemsViewModel
start a coroutine and when saving work is done emit event to navigate to main menuNikola Milovic
04/09/2021, 9:12 AMMarko Novakovic
04/09/2021, 9:13 AMuli
04/09/2021, 11:41 AMbinding.exitButton.setOnClickListener {
uiScope.launch{
autoSave()
viewModel.navigateToMainMenu()
}
}
suspend fun autoSave() {
viewModel.autoSave()
}
And depending on the rest of your code you can remove autoSave() (inline it)binding.exitButton.setOnClickListener {
uiScope.launch{
viewModel.autoSave()
viewModel.navigateToMainMenu()
}
}
Marko Novakovic
04/09/2021, 12:39 PMuli
04/09/2021, 2:57 PM