Nested transformation instead of merged livedatas

77 Views Asked by At

I have two livedatas startedAt & finishedAt and want to use them in a transformation that depend on both these values...i tried combine them using mediatorliveData

    fun <A, B> LiveData<A>.combine(other: LiveData<B>): PairLiveData<A, B> {
    return PairLiveData(this, other)
}

class PairLiveData<A, B>(first: LiveData<A>, second: LiveData<B>) : MediatorLiveData<Pair<A?, B?>>() {
    init {
        addSource(first) { value = it to second.value }
        addSource(second) { value = first.value to it }
    }
}

and observe them

  val automaticActivities = Transformations.map(startedAt.combine(finishedAt)) {
Log.v("checkindex",index.toString()+"    "+it.first?.size+"      "+it.second?.size)
 }

but this doesn't work as expected as finishedAt returns null before it get its required value ,

debugged rsult :

V/checkindex: 0 1 null

V/checkindex: 0 1 1

will it be a good idea to use a transformation inside a transformation ? ex:

val automaticActivities = Transformations.map(startedAt) {
   val finishedAt  = Transformations.map(finishedAt){
  }
}
0

There are 0 best solutions below