Eu sunt, folosind first
funcția de kotlin flux. Motivul pentru care eu sunt, folosind acest first
funcția este că nu trebuie să colecteze după prima dată.
Dacă nu întoarce nici o valoare booleană, se face roșu subliniază că trebuie să se întoarcă o valoare booleană. Ce ar trebui să mă întorc? Nu există nici o problemă atunci când mă voi întoarce adevărat, dar vreau să știu ce înseamnă.
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
Cod.
/**
* 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
}