Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda