2

I am looking to find a way to combine some LiveData functions from my Dao and transform them into a single entity. I want to create something like this:

private val combinedValues(ld1, ld2, ld3, ld4){
   first, second, third, fourth -> CombinedLiveDataValues(first, second, third, fourth)
}

val combinedEntity: LiveData<Any> = Transformations.map(combinedValues){ it->
   val something = it.first.map (etc...)
}

How to create a way of not duplicating code and generalising and adding dozens of liveDatas together?

RFM
  • 107
  • 7
  • Possible duplicate of [Is it possible to make one LiveData of two LiveDatas?](https://stackoverflow.com/questions/55044298/is-it-possible-to-make-one-livedata-of-two-livedatas) – EpicPandaForce Oct 02 '19 at 17:08

1 Answers1

-1

You can use something like this

fun<T> combine(context: AppCompatActivity, vararg input: LiveData<T>): LiveData<T> {
    val output = MutableLiveData<T>()
    input.forEach {
        it.observe(context, androidx.lifecycle.Observer { value -> 
            output.value = value
        })
    }
    return output
}
Ninja420
  • 3,542
  • 3
  • 22
  • 34