InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.98k stars 710 forks source link

viewModel() inside @Composable doesn't sync with sharedViewModel #1062

Closed Hachimori closed 2 years ago

Hachimori commented 3 years ago

Describe the bug Inside Fragment, I defined a sharedViewModel and called ViewModel's method to dispatch data via ViewModel's LiveData. Inside @Composable, I called getViewModel() and then did viewModel.liveData.observeAsState() to retrieve the data from ViewModel, however, the data cannot be retrieved.

To Reproduce Steps to reproduce the behavior:

  1. Inside Fragment, define ViewModel with by sharedViewModel().
  2. Call ViewModel's method and dispatch data using LiveData.
  3. Inside @Composable, get ViewModel with getViewModel().
  4. Try to get data from LiveData with viewModel.liveData.observeAsState().

Expected behavior Data is retrieved from ViewModel.

Koin project used and used version (please complete the following information):

implementation 'io.insert-koin:koin-core:3.0.1-beta-1'
implementation 'io.insert-koin:koin-android:3.0.1-beta-1'
implementation 'io.insert-koin:koin-androidx-compose:3.0.1-beta-1'
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.