मैं का उपयोग कर रहा हूँ first
समारोह kotlin से प्रवाह. कारण क्यों मैं इस का उपयोग कर 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
}