0

我正在尝试将 3 个或更多 LiveData 作为源合并,其中每个 LiveData 的通用类型与其他的不同。为此,我的意图是使用每个 LiveData 以其各自类型接收的可变参数。

我已经解决了这个问题,但是对 3 个数据源使用了一个“硬代码”,其解决方案与这个答案的解决方案非常相似,但这是不可扩展的。

使用意图如下(不编译):

    fun getSourceR(): MutableLiveData<Object_R>() {}
    fun getSourceS(): MutableLiveData<Object_S>() {}
    fun getSourceU(): MutableLiveData<Object_U>() {}

    MergeLiveDataTest(getSourceR(), getSourceS(), getSourceU()) {
        sources ->
            Log.d("MergeLiveData", sources[0].value.methodOf_R)
            Log.d("MergeLiveData", sources[1].value.methodOf_S)
            Log.d("MergeLiveData", sources[2].value.methodOf_U)
    }

问题:我无法访问源类型对象的方法。


到目前为止我的实现如下:

我正在尝试做的是使循环 X 的每次迭代为 R、S 或 U。

class MergeLiveDataTest<T, X>(
        vararg sources: LiveData<X>,
        private val onChanged: (newData: MutableList<LiveData<X>>) -> T
) : MediatorLiveData<T>() {
    private lateinit var _sources: MutableList<LiveData<X>>
    init {
        for (i in sources.indices) {
            super.addSource(sources[i]) {
                _sources.add(i, sources[i])
                value = onChanged(_sources)
            }
        }
    }
}

有什么建议吗?谢谢

资源

4

1 回答 1

1

我已经解决了这个问题,但是对 3 个数据源使用了一个“硬代码”,其解决方案与这个答案的解决方案非常相似,但这是不可扩展的。

当然是,你没看过编辑吗?;)

您可以使用我为此特定目的编写的库 https://github.com/Zhuinden/livedata-combinetuple-kt它做同样的事情。

现在仅链接的答案不是一件好事,所以我将解释它的作用。

首先我有 4-16 元组的元组(因为 Kotlin 已经有Pairand 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-stdlibPairTriple.

无论如何,我认为将 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)
}

我建议你应该尝试一下。

于 2020-04-27T20:59:26.137 回答