Hi guys, hope you are all doing well. I have a que...
# flow
d
Hi guys, hope you are all doing well. I have a question about
merge
function of Kotlin flows. In the docs here it says that it doesn't preserve the order of elements. Do you maybe know of some function that merges multiple flows and preserves the order of elements?
d
I don't understand how can this help me? I need to merge 4-5 flows into one flow and get its valeus
does the point mentioned it doesn't preserve the order of elements mean that elements can come in wrong order or something else? Let's say I combine
Flow<Int>
and
Flow<String>
Let's say
Int
is emitted and then
String
is emitted What I understood is that the `merge`d flow that I collect can first collect
String
and then
Int
even though
Int
was emitted first
j
Sorry not sure if I understand. Have a look at this example
Copy code
fun test() = flow {
        listOf(
            (1..5).asFlow(),
            (6..10).asFlow(),
            (11..15).asFlow(),
            (16..20).asFlow(),
            (21..25).asFlow(),
        ).map { emitAll(it) }
    }

    test().collect { println(it) }

    merge(
        (1..5).asFlow(),
        (6..10).asFlow(),
        (11..15).asFlow(),
        (16..20).asFlow(),
        (21..25).asFlow(),
    ).collect { println("Merge ---- $it") }