0

我正在从数据库中加载 Flow>。基于此结果,我想将每个元素 a 与另一个数据库调用转换为 Pair。

所以基本上是这样的:

dao.getElementAList().map { list ->
 list.map {elementA -> Pair(it,dao.call2(elementA)) }
}

调用 2 还返回一个带有 ElementB 的流。返回类型必须是 List>。

如何使用 Kotlin Flow API 实现这一目标?

4

1 回答 1

0

先决条件

摇篮

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7")

类/接口

interface Database {
    fun getElementAList(): Flow<List<Element>>

    fun call2(element: Element): Flow<Call2>
}

class DatabaseImpl : Database {
    override fun getElementAList(): Flow<List<Element>> {
        return flowOf(listOf(Element("1"), Element("2"), Element("3")))
    }

    override fun call2(element: Element): Flow<Call2> {
        return listOf(Call2("c1")).asFlow()
    }
}

data class Element(val s: String) {}

data class Call2(val s: String) {}

解决方案

dao.getElementAList()
    .flatMapConcat { it.asFlow() }
    .flatMapMerge { e -> dao.call2(e).map { c -> e to c } }

注意:结果是流式传输的,不会立即作为列表发出。如果一个调用失败,则所有调用都失败。

测试

@ExperimentalCoroutinesApi
@FlowPreview
@Test
fun sdfdsf() {
    val dao = DatabaseImpl()

    runBlockingTest {
        val result = dao.getElementAList()
                .flatMapConcat { it.asFlow() }
                .flatMapMerge { e -> dao.call2(e).map { c -> e to c } }
                .toList(mutableListOf())

        assertThat(result)
                .containsExactly(
                        Element("1") to Call2("c1"),
                        Element("2") to Call2("c1"),
                        Element("3") to Call2("c1")
                )
    }
}
于 2020-06-11T11:19:05.957 回答