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

124
Views
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 answers
Answer question

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 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!