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 triggers with the old value inside ViewModel injected using SharedViewModel #813

Closed Marchosiax closed 2 years ago

Marchosiax commented 4 years ago

Describe the bug I'm not sure if this is a koin bug or LiveData bug or even a bug in general! I'm using Jetpack Navigation with one activity and bunch of framents and a view model which is shared between these fragmenst. Fragment A calls a http request, observes the data and if the condition is true, it navigates to fragment B. Now if I navigate back to fragment A, it observes that data again and navigates to fragment B. In other word, every time i try to go back to A, becasue the old data is still there, it shoots me back to the B. I tried using the by viewModel injection but the same thing happed. I tried observing inside onCreate() but it doesn't work because the viewLifeCyclerOwber is still null.

class InitialUserDataFragment : Fragment(R.layout.fragment_initial_user_data) {

    private val viewModel by sharedViewModel<KYCViewModel>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    private fun init() {
        viewModel.localInquiryResponse.observe(viewLifecycleOwner) { response ->
            if (response.isNotInProgress()) {
                btnAccept.enable()
                hideLoading()
            }

            handleResponse(response) {
                if (it?.processStatus == null)
                    next(this)
                else
                    gotToResult()

            }
        }
}

To Reproduce

  1. Call http request in fragment A then navigate to fragment B
  2. Try to navigate back
  3. Fragment A observes the old data and navigates to B without even calling the http request.

Expected behavior Fragment A does not observe old data again

Koin project used and used version (please complete the following information): org.koin:koin-android:2.1.5 org.koin:koin-android-scope:2.1.5 org.koin:koin-android-viewmodel:2.1.5

MrSatan commented 3 years ago

use SingleLiveEvent .

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.