Sto usando first
funzione da kotlin flusso. Il motivo per cui sto usando questo first
la funzione è che non devo raccogliere dopo la prima volta.
Se non mi restituisce alcun valore booleano, si rende rosso sottolineare che devo restituire un valore booleano. Che cosa devo restituire? Non esiste nessun problema, quando ho restituisce true, ma voglio sapere che cosa significa.
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
Codice.
/**
* 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
}