I was chasing a bug where a viewmodel was created ...
# android
m
I was chasing a bug where a viewmodel was created twice when using `by viewModels`:
Copy code
class MyFragment : Fragment() {

    private val viewModel by viewModels<MyViewModel> {
        MyViewModelProvider(App.instance)
    }
I logged the constructors of the Fragment as well as the viewmodel:
Copy code
2021-10-29 12:17:44.494 1026-1026 [main] Create MyFragment hashCode=232615119
2021-10-29 12:17:44.732 1026-1026 [main @coroutine#172] Create MyViewModel hashCode=113130906
2021-10-29 12:17:44.732 1026-1343 [DefaultDispatcher-worker-5 @coroutine#181] Create MyViewModel hashCode=140268101
I found the reason to be that I use the viewModel from different Dispatchers, I had one flow of the viewModel collected inside launch(Dispatchers.Default). Is this now allowed / documented somewhere, or is this a bug?