0

I have a question around Single and haven't been able to find a good answer for that yet. I have to return a Single from a method where I get 2 Single sources. The problem is I need to use the output of the 2 singles to modify a class and then send it back. Ideally, it should be something like Observable.combineLatest but I haven't found a good answer yet.

data class A (val resultX : Int, val resultY: Int)

I have 2 sources of Single that fills up the A object.

fun resultX() : Single<Int>
fun resultY() : Single<Int>

What I want to do is combine the results of the above 2 Single and send back a result A object.

Single.<blah>(resultX(), resultY()) { resultX, resultY -> A(resultX, resultY)}

Is there a method that could help me combine these? Thanks!

Progman
  • 16,827
  • 6
  • 33
  • 48

2 Answers2

1

Yes, you can do it in two ways:

Single.zip(resultX(), resultY()) { resultX, resultY -> A(resultX, resultY) }

or

resultX().zipWith(resultY()) { resultX, resultY -> A(resultX, resultY) }
Alexander
  • 316
  • 1
  • 7
0

Looks like Single.zip did the trick.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '23 at 09:19