https://kotlinlang.org logo
Title
j

Joan Colmenero

07/27/2020, 11:04 AM
Hello how do I subscribe to an
Observable
and
Single
at the same time? I have this
Observable.zip(repo1.getStream(), repoSingle.getStream().toObservable(), (repo1, repo2) -> repo1)
But it's not calling, the thing is that I'd like to execute both and subscribe to both in the same
subscribe
it works if I create two separate subscribe though. Also I need to use the result of the
repo1.getStream()
to make some calls from the result like if is returning an user I'd like to do user -> user.getFoo(). Thanks
g

gildor

07/27/2020, 11:06 AM
But do you understand that if you combine Observable and single, this observablew finish when single success?
j

Joan Colmenero

07/27/2020, 11:07 AM
Yes I guess this is the problem why is not working..
g

gildor

07/27/2020, 11:07 AM
so it’s expected
j

Joan Colmenero

07/27/2020, 11:07 AM
I read about
doOnDispose()
also but don't know how to make it work
g

gildor

07/27/2020, 11:07 AM
why do you need doOnDispose?
what is you expected semantics?
j

Joan Colmenero

07/27/2020, 11:08 AM
Because when one finishes it calls the doOnDispose as far as I read on the documentation
g

gildor

07/27/2020, 11:08 AM
yes
but not when it finishes
but when it disposed
j

Joan Colmenero

07/27/2020, 11:08 AM
well the thing is I'd like to avoid this
this observablew finish when single success?
And wait until both are done
g

gildor

07/27/2020, 11:08 AM
to subscribe on finish use doFinally
And wait until both are done
But this code works like this
j

Joan Colmenero

07/27/2020, 11:09 AM
If it's possible because if I create two subscribe it works but looks ugly to me
g

gildor

07/27/2020, 11:09 AM
so after your single is finish it’s fine to stop observing repo1?
zip waits when both streams emit at least 1 value
but it will be completed if one of them complete
so it probabably what you want, but if you need only 1 value, I would recommend instead Single.zip
j

Joan Colmenero

07/27/2020, 11:13 AM
The thing is normally I was using observable but then I realized that I need that Single to make it work
g

gildor

07/27/2020, 11:13 AM
It would be a bit more clear semantically and it checks that at least 1 value is emmitted
j

Joan Colmenero

07/27/2020, 11:13 AM
Because more values can be emited (by the observable) single is fire and forget
But I need to call it to update some UI
g

gildor

07/27/2020, 11:14 AM
anyway, I would recommend to write some sample and demonstrate what doesn’t work for you
j

Joan Colmenero

07/27/2020, 11:14 AM
The other needs to be there
g

gildor

07/27/2020, 11:14 AM
because it’s not clear what semantics you excpect
j

Joan Colmenero

07/27/2020, 11:17 AM
Ok the thing is, this Observable (repo1) doesn't need to stop, but this Single(repo2) is only to make sure some UI is updated, so I thought about making a Zip and then just use the result of the repo1(Observable) and forget about repo2(Single)
What I had before is only this Observable and I was using the result (user) and updating UI with that user, now, I need to call another repository (Single) but it's doing the changes internally so it's like I don't need to act with it
g

gildor

07/27/2020, 12:08 PM
Use combineLatest instead of zip, it will not stop observable when single success, it will emit both values every time when observable emits
j

Joan Colmenero

07/27/2020, 12:10 PM
I'll try thanks
But I have to put it with specific order?
g

gildor

07/27/2020, 12:28 PM
Specific order?
Ahhh, I got it, it will just change order of params in lambda, same as with zip
j

Joan Colmenero

07/27/2020, 12:31 PM
So the syntax is Observable.combineLatest(repo1.getStream, repo2.getInfo)?
g

gildor

07/27/2020, 12:37 PM
There are many different overloads, check javadoc
You also should provide combining lambda as latest argument,same as for zip
j

Joan Colmenero

07/27/2020, 12:44 PM
I think I got it, let me test it and I'll come back 🙂
Thanks