InsertKoinIO / koin

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

Livedata in activityViewModel not updated #1781

Closed funkyidol closed 4 months ago

funkyidol commented 9 months ago

Describe the bug I have a view model containing livedata is shared between an Activity and fragment. I update the livedata in the activity and observe it in fragment via activityViewModel. The expected beaviour is that the viewmodel observer should fire when viewmodel is updated in the activity, which is not happening.

This issue is only visible in koin version 3.5.3. Its not happening in 3.5.0

To Reproduce Steps to reproduce the behavior:

  1. Create a viewmodel with mutable live data
  2. Consume the viewmodel in activity via by viewModel()
  3. Write the live data update code in activity [Add debug point]
  4. Consume the same view model is fragment via by activityViewModel()
  5. Write the live data observer [Add debug point]
  6. Run the app is debug mode
  7. Observe that the first debug point in activity is reached but second does not.

Koin module and version: koin-core:3.5.3

Snippet or Sample project to help reproduce

class ActivitySharedViewModel : ViewModel() {

    private val _isEnabledEvent = MutableLiveData<Event<Boolean>>()
    val isEnabledEvent: LiveData<Event<Boolean>> = _isEnabledEvent

    fun setIsEnabled(isEnabled: Boolean){
        _isEnabledEvent.value = Event(isEnabled)
    }

}

// Fragment Code
activitySharedViewModel.isEnabledEvent.observeEvent(viewLifecycleOwner) {
     isEnabled = it
}

// Activity Code
        activitySharedViewModel.setIsEnabled(true)
stale[bot] commented 4 months 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.