0

I am observer LiveData from Repository and transform to view,here Transformations.switchMap Function not called in ViewModel. If I use observeForEver it works good.

Transformations.switchMap(signInResponseMutableLiveData){signInResponse ->
            val mutableLiveData = MutableLiveData<Any>()

            if (signInResponse is FirebaseUser) {
                val firebaseUserEmail = signInResponse.email

                homeFragmentUIDataLiveData.value = HomeFragmentUIData(firebaseUserEmail ?: "")
            } else if (signInResponse is String) {
                loginErrorEventLiveData.value = signInResponse
            }

            mutableLiveData
        }
Ramprasad
  • 7,981
  • 20
  • 74
  • 135
  • Actually it is available in ViewModel class and observes LiveData from Repository and update data to UI. switchMap should return LiveData thats the reason using dummy LiveData.My main aim is update homeFragmentUIDataLiveData and loginErrorEventLiveData which are observed in Fragment. – Ramprasad Sep 15 '18 at 11:14
  • To transform data from one LiveData to another LiveData – Ramprasad Sep 15 '18 at 11:24
  • observe method needs LifecycleOwner but I need to call from ViewModel – Ramprasad Sep 15 '18 at 11:29
  • If use observeForever we should remove observer manully, then what is the use of using Architecture components? I think something missing in switchMap,because I already implemented this pattern in java and it works great. https://developer.android.com/reference/android/arch/lifecycle/LiveData.html#observeForever(android.arch.lifecycle.Observer%3CT%3E) – Ramprasad Sep 15 '18 at 11:38
  • Even it is used inside switchMap, we don't need to do it manually.That is the reason to avoid common mistakes. – Ramprasad Sep 15 '18 at 11:49
  • Okay I use switchMap – Ramprasad Sep 15 '18 at 11:56
  • 2
    have you found a solution? – Andrey Solera Oct 07 '19 at 03:39

0 Answers0