title: Can an Android Architecture Components ViewModel compose an object from multiple LiveData returning models?
body: <p>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 to hide the model details from the Fragment and respond to external data changes through the individual models.</p>
  
  <p>The problem is I need the ViewModel to observe the model changes but ViewModel is not a LifecycleOwner so it can't observe. Since I don't want to pass the LiveData objects through to the UI, I'm stuck.</p>
  
  <p>Is this possible? Do I need to abandon LiveData for my models and resort to a different observation pattern / tool?</p>
  
  <p><strong>Edit:</strong> Pseudocode added. My actual classes are much more complex and lengthy. I hope my intent is understandable.</p>
  
  <pre><code>// OneDataModel.kt
  class oneDataModel {
      val oneDataElement = ""
  }
  
  // AnotherDataModel.kt
  class anotherDataModel {
      val anotherDataElement = 19
  }
  
  // OneDataRepository.kt
  class OneDataRepository {
      val oneDataSet = MutableLiveData&lt;MutableList&lt;oneDataModel&gt;&gt;()
  
      private val dataListener = object: ChildEventListener {
          override fun onChildAdded(snapshot: DataSnapshot, p1: String?) {
              val newChild = snapshot.getValue(oneDataModel::class.java)
              if (newChild != null) {
                  oneDataSet.value?.add(newChild)
              }
          }
      }
  
      init {
          oneDataSet.value = mutableListOf&lt;oneDataModel&gt;()
          OneNetworkDataTable.addListener(dataListener)
      }
  }
  
  // AnotherDataRepository.kt
  class AnotherDataRepository {
      var anotherDataSet = MutableLiveData&lt;MutableList&lt;anotherDataModel&gt;&gt;()
  
      private val dataListener = object: ChildEventListener {
          override fun onChildAdded(snapshot: DataSnapshot, p1: String?) {
              val newChild = snapshot.getValue(anotherDataModel::class.java)
              if (newChild != null) {
                  anotherDataSet.value?.add(newChild)
              }
          }
      }
  
      init {
          anotherDataSet.value = mutableListOf&lt;anotherDataModel&gt;()
          AnotherNetworkDataTable.addListener(dataListener)
      }
  }
  
  // ComposedViewModel.kt
  class ComposedViewModel: ViewModel() {
      class ComposedItem {
          var dataName: String = ""   // From OneDataRepository items
          var dataValue: Int = -1     // From AnotherDataRepository items
      }
      var publishedDataSet = MutableLiveData&lt;MutableList&lt;ComposedItem&gt;&gt;()
  
      //***
      //*** WHAT GOES HERE? HOW DO I LISTEN TO EACH OF THE DATA REPOSITORIES AND BUILD UP COMPOSED
      //*** ITEMS FOR THE UI?
      //***
  }
  
  // MyFragment.kt
  class MyFragment : Fragment() {
      private val composedViewModel: ComposedViewModel by lazy { ViewModelProviders.of(activity).get(ComposedViewModel::class.java) }
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          val view = inflater.inflate(R.layout.fragment_feed, container, false)
  
          recyclerView.adapter = UIAdapter
  
          composedViewModel.publishedDataSet.observe(this, Observer {
              UIAdapter.notifyDataSetChanged()
          })
  
          return view
      }
  }
  </code></pre>