내가 사용하고 first
함수 코 틀린 흐름입니다. 왜 내가 사용하여 이 first
기능는 수집하지 않아도 첫 시간입니다.
내가 돌아오지 않으면 어떤 부울 값을,그것은 붉은 밑줄을 나는 부울 값을 반환합니다. 어떻게 해야 하는가? 이 없 문제가하면 true 를 반환하지만,무엇인지 알고 싶은 그것이 의미하는 것입니다.
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
}