更改Flowable <list <obj1>&gt;流动<list <obj2>&gt;在房间里

时间:2017-08-27 19:31:00

标签: android kotlin rx-java2 android-room

如何从房间读取可流动的值列表并将其转换为另一个对象,该对象是来自房间的更多值的组合

database.leadsDao().getLeads(leadState.name)
    .flatMap {
        val len = it.size.toLong()
        Flowable.fromIterable(it)
            .flatMap {
                Flowable.zip(
                        database.orderDao().getById(it.orderId),
                        database.orderMedicineDao().getByOrderId(it.orderId),
                        database.patientDao().getById(it.patientId),
                        Function3<Order, List<OrderMedicine>, Patient, LeadDetail>
                        { order, orderMedicines, patient -> LeadDetail.from(it, patient, order, orderMedicines) })
            }
            .take(len)
            .toList()
            .toFlowable()
    }

上面的代码有效,但我不喜欢take(len)部分。没有它,流永远不会调用订阅者的下一个。流不断等待更多项,这不应该发生,因为Flowable.fromIterable给出有限数量或项目然后结束。即,下面的代码不起作用

database.leadsDao().getLeads(leadState.name)
    .flatMap {
        Flowable.fromIterable(it)
            .flatMap {
                Flowable.zip(
                        database.orderDao().getById(it.orderId),
                        database.orderMedicineDao().getByOrderId(it.orderId),
                        database.patientDao().getById(it.patientId),
                        Function3<Order, List<OrderMedicine>, Patient, LeadDetail>
                        { order, orderMedicines, patient -> LeadDetail.from(it, patient, order, orderMedicines) })
            }
            .toList()
            .toFlowable()
    }

1 个答案:

答案 0 :(得分:0)

  

Flowable.fromIterable给出有限数量或项目然后结束。

Flowable.zip内的flatmap不会结束,因为Room的DAO对象会发出当前值和所有未来的更新,因此压缩的database.*()个调用在一起不是有限的。如果您向内部.first()添加Flowable.zip调用,则第二个版本也可以正常运行。