什么我应该已经返回科特林第一流的功能?

0

的问题

我使用 first 能从科特林的流动。 为什么我使用这个 first 功能是,我没有收集之后的第一次。 如果我不回任何布尔的价值,它使红强调,我必须返回布尔的价值。 我应该怎么回报? 没有任何问题,当我刚返回的真实的,但我想知道这意味着什么。

    private fun getGroupNameData() {
        viewModelScope.launch {
            repository.loadGroupsWithFlow()
                .buffer()
                .first { newList ->
                    groupData.clear()
                    newList.forEach { newGroupData ->
                        groupData[newGroupData.id] = newGroupData.name
                    }
                    true // <- what is this boolean value?
                }
        }
    }

first 代码。

/**
 * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
 * Throws [NoSuchElementException] if the flow has not contained elements matching the [predicate].
 */
public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
    var result: Any? = NULL
    collectWhile {
        if (predicate(it)) {
            result = it
            false
        } else {
            true
        }
    }
    if (result === NULL) throw NoSuchElementException("Expected at least one element matching the predicate $predicate")
    return result as T
}
1

最好的答案

2

这种超负荷的 Flow.first() 是用来让第一个值的流动相匹配给定谓词。 这就是为什么lambda希望你回布尔结束。 任何价值的氧返回真的,值将返回和流将被取消。

如果你只需要第一数值,则应的其他重载它不接受一个谓lambda。

val newList = repository.loadGroupsWithFlow().buffer().first() // Use this first()
groupData.clear()
newList.forEach { newGroupData ->
    groupData[newGroupData.id] = newGroupData.name
}

顺便说一句我不认为的缓冲区是必需的。 你可以删除。

2021-11-24 06:26:16

谢谢你,你能不能解释为什么缓冲的是不需要?
Lee WonJoong

你可以看看它的 文件. 在你的种情况下你只关心的第一个值中发现由流动,所以你不需要缓冲区的任何东西。
Arpit Shukla

哦我明白了。 我只需要第一个价值,我不需要缓冲区。 谢谢你!
Lee WonJoong

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................