Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Combine a Flow and a non Flow api response Kotlin

I currently have a piece of logic as follows:

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

The problem I am having is that, if repository.getSomeThings has been triggered multiple times before, anotherRepository.getThings is getting triggered for the amount of all the pre-loaded values from repository.getSomeThings. I was wondering what is the proper way to use these repositories, one a suspend function, the other a Flow together. The equivalent behaviour that is combineLatest{} in Rx.

Thank you.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

There are a couple of ways to solve your problem. One way is just to call repository.getSomeThings() in the collectLatest block and cache last result:

var lastResult: AsyncResult<SomeThings>? = null

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

Another approach is to create a Flow, which will be calling repository.getSomeThings() function, and combine two Flows:

combine(
  anotherRepository.getThings(),
  flow {emit(repository.getSomeThings())}
) { result1: List<String>, result2: AsyncResult<SomeThings>  ->
  ...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda