How to Return LiveData from Repository

后端 未结 3 1935
日久生厌
日久生厌 2020-12-22 08:52

I just can\'t see how to chain LiveData from Repo to VM, so I have tried to boil this down to the most simple of example!:

Fragment

         


        
3条回答
  •  醉话见心
    2020-12-22 09:33

    No need to change MutableLiveData inside ViewModel. Try to pass whatever Repository send to View. Check below

    class LoginViewModel : ViewModel() {
    
        private val firestoreRepository : FirestoreRepository = FirestoreRepository()
    
        fun getCurrentName(): MutableLiveData {
            return firestoreRepository.getCurrentName()
        }
    
        fun changeText() {
            firestoreRepository.changeText()
        }
    }
    

    And also your FirestoreRepository

    class FirestoreRepository {
    
        private val mCurrentName = MutableLiveData()
    
        fun getCurrentName(): MutableLiveData {
            return mCurrentName
        }
    
        fun changeText() {
            mCurrentName.value = "Button Clicked!!"
        }
    }
    

提交回复
热议问题