voximdo
voximdo

Reputation: 63

ViewModelScope is canceled permanently?

I am trying to use MVVM in my latest Android app. I am also using coroutines. I have ViewModel, that is injected into Activity using koin. To run coroutines in my ViewModel I am using ViewModelScope. Then after Activity is finished, ViewModel is cleared, and I will run this activity again - viewModelScope is canceled since the beginning.

That's very odd. As viewModelScope should be... reseted somehow or something? Or maybe my viewModelScope isn't closing correctly?

Upvotes: 1

Views: 507

Answers (1)

Indiana
Indiana

Reputation: 723

It would be helpful to see some code, but one possible thing to consider is - Are you injecting your ViewModel as a singleton with Koin?

single { MyViewModel() }

If so, this is your issue, as Koin is creating a single instance of your viewmodel and using this when you next load your activity. Change your Koin module to use the viewModel injection like so:

viewModel { MyViewModel() }

Upvotes: 4

Related Questions