problem_asker_2022
problem_asker_2022

Reputation: 13

Combine Singles and generate a data class Single

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!

Upvotes: 0

Views: 45

Answers (2)

Alexander
Alexander

Reputation: 314

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

Upvotes: 1

problem_asker_2022
problem_asker_2022

Reputation: 13

Looks like Single.zip did the trick.

Upvotes: 0

Related Questions