我已经解决了这个问题,但是对 3 个数据源使用了一个“硬代码”,其解决方案与这个答案的解决方案非常相似,但这是不可扩展的。
当然是,你没看过编辑吗?;)
您可以使用我为此特定目的编写的库 https://github.com/Zhuinden/livedata-combinetuple-kt它做同样的事情。
现在仅链接的答案不是一件好事,所以我将解释它的作用。
首先我有 4-16 元组的元组(因为 Kotlin 已经有Pair
and Triple
)在tuples-kt
(我写了这样你就不需要自己写这么多元组)
它们看起来像这样:
data class Tuple4<A, B, C, D>(
val first: A,
val second: B,
val third: C,
val fourth: D
) : Serializable {
override fun toString(): String {
return "Tuple4[$first, $second, $third, $fourth]"
}
}
data class Tuple5<A, B, C, D, E>(
val first: A,
val second: B,
val third: C,
val fourth: D,
val fifth: E
) : Serializable {
override fun toString(): String {
return "Tuple5[$first, $second, $third, $fourth, $fifth]"
}
}
然后在LiveData-CombineTuple-KT
,有这样的代码:
fun <T1, T2, T3, T4> combineTuple(f1: LiveData<T1>, f2: LiveData<T2>, f3: LiveData<T3>, f4: LiveData<T4>): LiveData<Tuple4<T1?, T2?, T3?, T4?>> = MediatorLiveData<Tuple4<T1?, T2?, T3?, T4?>>().also { mediator ->
mediator.value = Tuple4(f1.value, f2.value, f3.value, f4.value)
mediator.addSource(f1) { t1: T1? ->
val (_, t2, t3, t4) = mediator.value!!
mediator.value = Tuple4(t1, t2, t3, t4)
}
mediator.addSource(f2) { t2: T2? ->
val (t1, _, t3, t4) = mediator.value!!
mediator.value = Tuple4(t1, t2, t3, t4)
}
mediator.addSource(f3) { t3: T3? ->
val (t1, t2, _, t4) = mediator.value!!
mediator.value = Tuple4(t1, t2, t3, t4)
}
mediator.addSource(f4) { t4: T4? ->
val (t1, t2, t3, _) = mediator.value!!
mediator.value = Tuple4(t1, t2, t3, t4)
}
}
fun <T1, T2, T3, T4, T5> combineTuple(f1: LiveData<T1>, f2: LiveData<T2>, f3: LiveData<T3>, f4: LiveData<T4>, f5: LiveData<T5>): LiveData<Tuple5<T1?, T2?, T3?, T4?, T5?>> = MediatorLiveData<Tuple5<T1?, T2?, T3?, T4?, T5?>>().also { mediator ->
mediator.value = Tuple5(f1.value, f2.value, f3.value, f4.value, f5.value)
mediator.addSource(f1) { t1: T1? ->
val (_, t2, t3, t4, t5) = mediator.value!!
mediator.value = Tuple5(t1, t2, t3, t4, t5)
}
mediator.addSource(f2) { t2: T2? ->
val (t1, _, t3, t4, t5) = mediator.value!!
mediator.value = Tuple5(t1, t2, t3, t4, t5)
}
mediator.addSource(f3) { t3: T3? ->
val (t1, t2, _, t4, t5) = mediator.value!!
mediator.value = Tuple5(t1, t2, t3, t4, t5)
}
mediator.addSource(f4) { t4: T4? ->
val (t1, t2, t3, _, t5) = mediator.value!!
mediator.value = Tuple5(t1, t2, t3, t4, t5)
}
mediator.addSource(f5) { t5: T5? ->
val (t1, t2, t3, t4, _) = mediator.value!!
mediator.value = Tuple5(t1, t2, t3, t4, t5)
}
}
从 3 一直写到 16。虽然它是一个 Kotlin 库,所以它假设您的项目中有 Kotlin-stdlibPair
和Triple
.
无论如何,我认为将 16 个 LiveData 组合成一个元组的能力对于大多数情况来说应该足够了。
如果不允许使用 Kotlin,那么我确信相关的 Kotlin 逻辑可以翻译成 Java,但它会更加冗长,所以我从来没有这样做过。
在 Kotlin 中,您现在可以轻松地做到这一点:
val liveData = combineTuple(liveData1, liveData2, liveData3).map { (value1, value2, value3) ->
// do something with nullable values
}
liveData.observe(this) { mappedValue ->
// do something with mapped value
}
您的示例将更改如下
fun getSourceR(): MutableLiveData<Object_R>() {}
fun getSourceS(): MutableLiveData<Object_S>() {}
fun getSourceU(): MutableLiveData<Object_U>() {}
combineTuple(getSourceR(), getSourceS(), getSourceU()).map { (r, s, u) ->
sources ->
Log.d("MergeLiveData", r?.methodOf_R)
Log.d("MergeLiveData", s?.methodOf_S)
Log.d("MergeLiveData", u?.methodOf_U)
}
我建议你应该尝试一下。