【问题标题】:Compare two flowable streams in Rx比较 Rx 中的两个可流动的流
【发布时间】:2019-05-17 05:53:16
【问题描述】:

我有两张桌子。表 A 和表 B。我正在使用带有 Room 的 Android 和带有 ktx 的 Reactive Streams。

The Table A has two columns Title, Ids.
Row 1 - ['example', '1,2,3,4'].
The Table B has two columns Id, Desc. 
Row 1 - [1, 'long desc']
Row 2 - [2, 'long desc 2'].

我正在使用 Flowable 从数据库中获取数据,但它们是两个不同的流。

如何获取表 B 中的行列表,这些行在表 A 中具有 ID。表 A 将表 B 的 ID 存储为字符串。

【问题讨论】:

  • 您可以编写一个 observable 从表 A 中获取 Id,然后使用 flatMap 运算符从表 B 中获取该 Id 的 Observable

标签: android kotlin android-room reactive-streams


【解决方案1】:

我不知道您的用例的详细信息,但您的意思是这样吗?

interface TableADao {
    fun findById(id: Long): Flowable<ItemA>
}

interface TableBDao {
    fun findByIds(ids: Array<Long>): Flowable<List<ItemB>>
}

tableADao.findById(id)
    .switchMap { itemA ->
        val ids = someLongArrayConverter(itemA.ids)
        tableBDao.findByIds(ids)
    }
    .subscribe { itemBList ->
        Log.d("result", itemBList)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多