Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Combine una respuesta de API de flujo y no de flujo Kotlin

Actualmente tengo una parte de la lógica de la siguiente manera:

 interface anotherRepository { fun getThings(): Flow<List<String>> }
 interface repository { suspend fun getSomeThings(): AsyncResult<SomeThings> }
 when (val result = repository.getSomeThings()) { is AsyncResult.Success -> { anotherRepository.getThings().collectLatest { // update the state } else -> { } } }

El problema que tengo es que, si repository.getSomeThings se ha activado varias veces antes, se activa anotherRepository.getThings por la cantidad de todos los valores precargados de repository.getSomeThings . Me preguntaba cuál es la forma correcta de usar estos repositorios, uno como función de suspensión, el otro como Flujo juntos. El comportamiento equivalente que es combineLatest{} en Rx.

Gracias.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hay un par de maneras de resolver su problema. Una forma es simplemente llamar a repository.getSomeThings() en el bloque collectLatest y almacenar en caché el último resultado:

 var lastResult: AsyncResult<SomeThings>? = null anotherRepository.getThings().collectLatest { if (lastResult == null) { lastResult = repository.getSomeThings() } // use lastResult and List<String> }

Otro enfoque es crear un Flow , que llamará a la función repository.getSomeThings() y combine dos flujos:

 combine( anotherRepository.getThings(), flow {emit(repository.getSomeThings())} ) { result1: List<String>, result2: AsyncResult<SomeThings> -> ... }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!