English 中文(简体)
Kotlin结合了2个以上的流动
原标题:Kotlin combine more than 2 flows

I m looking to combine 4 StateFlow values and make 1 StateFlow from these. I know already of the combine function like this:

val buttonEnabled = cameraPermission.combine(micPermission) {
    //some logic
}

如何利用4次流动来做到这一点? 当我尝试下文时,我发现错误时,有太多的论点,但综合职能,即,你确实能够增加多达5个流动?

val buttonEnabled = cameraPermission.combine(micPermission, locationPermission, contactsPermission) {

}
最佳回答

而“但联合职能......” 确实说,你能够增加多达5个流动资金?

是 syntax :

combine(流量1, 流量2, 流量3, 流量4){t1, t2, t3, t4 -> resultMapper}.stateIn(cop)

如果你需要5个以上的组合,那么就非常简单地为6个机构树立了自己的榜样:

fun <T1, T2, T3, T4, T5, T6, R> combine(
    flow: Flow<T1>,
    flow2: Flow<T2>,
    flow3: Flow<T3>,
    flow4: Flow<T4>,
    flow5: Flow<T5>,
    flow6: Flow<T6>,
    transform: suspend (T1, T2, T3, T4, T5, T6) -> R
): Flow<R> = combine(
    combine(flow, flow2, flow3, ::Triple),
    combine(flow4, flow5, flow6, ::Triple)
) { t1, t2 ->
    transform(
        t1.first,
        t1.second,
        t1.third,
        t2.first,
        t2.second,
        t2.third
    )
}
问题回答

您只能使用从Kotlinx.coroutines.flow.Zip.kt的组合功能。

例如。

combine(flow1, flow2, flow3) {f1,f2,f3-> // do something }

如果您需要将多种同类流动结合起来,不论时间长短,

您可使用这一方法制造

class Util {
    companion object {
        fun <T> getMultipleCombinedFlow(vararg flow: Flow<T>, transform: (T, T) -> T): Flow<T> {
            return combineMultiple(flow, 0, null, transform)
        }

        private fun <T> combineMultiple(
            array: Array<out Flow<T>>,
            index: Int = 0,
            combined: Flow<T>? = null,
            transform: (T, T) -> T
        ): Flow<T> {
            if (array.size == 1) {
                return array[0]
            }
            if (index >= array.size)
                return combined!!

            if (combined == null) {
                val result = array[index].combine(array[index + 1], transform)
                return combineMultiple(array, index + 2, result, transform)
            }

            val result = combined.combine(array[index], transform)
            return combineMultiple(array, index + 1, result, transform)

        }
    }
}

Feb 2024
These are the most direct approaches :

operator description
combine This operator takes multiple Flows and a transformation function as arguments. The transformation function receives the latest values from each flow and combines them into a single value emitted by the resulting flow.
Document
zip Similar to combine, but only emits when all input flows have emitted a value.
Useful when you need to react to events happening simultaneously across multiple flows.
Document

www.un.org/Depts/DGACM/index_french.htm

val flow1 = flow { ... }
val flow2 = flow { ... }

val combinedFlow = combine(flow1, flow2) { value1, value2 ->
    // combine values here
    Pair(value1, value2) 
}

scope.launch {
    combined.collect{
        Log.e("TAG: ","${it.first} , ${it.second}")
    }
}

www.un.org/Depts/DGACM/index_russian.htm

val flow1 = flow { ... }
val flow2 = flow { ... }
val flow3 = flow { ... }

val zippedFlow = flow1.zip(flow2, flow3) { value1, value2, value3 ->
    // combine values here
    Triple(value1, value2, value3)
}

scope.launch {
    zippedFlow.collect { (value1, value2, value3) ->
        // Use the zipped values
        Log.e("TAG: ","${it.first} , ${it.second} , ${it.third}")
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签