Cosa devo avere per tornare a kotlin Flusso prima funzione?

0

Domanda

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
}
1

Migliore risposta

2

Questo sovraccarico di Flow.first() è usato per ottenere il primo valore di flusso che corrisponde a un dato predicato. Ecco perché la lambda si aspetta che restituisca un boolean alla fine. Per qualsiasi valore di lambda restituisce true, il valore verrà restituito e il flusso sarà annullata.

Se è necessario solo il primo valore, si dovrebbe altra sovraccarico che non accetta un predicato lambda.

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

Btw non credo che il buffer è necessario. È possibile rimuovere.

2021-11-24 06:26:16

Grazie, mi Può spiegare perché il buffer non è necessario?
Lee WonJoong

Puoi dare un'occhiata alla sua documentazione. Nel tuo caso si preoccupano solo il primo valore emessa dal flusso in modo da non avete bisogno di buffer nulla.
Arpit Shukla

Oh, capisco. Come mi manca solo il primo valore, non ho bisogno di buffer. Grazie!
Lee WonJoong

In altre lingue

Questa pagina è in altre lingue

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