Can an Android Architecture Components ViewModel compose an object from multiple LiveData returning models?

后端 未结 2 1088
臣服心动
臣服心动 2021-01-03 11:05

I\'ve been unable to create an Android Architecuture Components ViewModel that composes multiple LiveData models into one LiveData class for my Fragment to observe. I want t

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 11:12

    I would suggest to take a different approach:

    • Use reactive streams (RxJava) to represent your model
    • Convert your RxJava Observables into LiveData using lifecycle-reactivestreams from the Architecture Components.

    By using RxJava you can easily merge observables together and only expose that combination to the view using LiveData. I would recommend to use LiveData only to expose state to the view (Fragment or Activity).

    I wrote an article on how to do this. You might find it useful.

提交回复
热议问题