Yes, you have turned it into machine that ignores ...
# rx
m
Yes, you have turned it into machine that ignores next events. You will only get terminal if subject errors or completes or you get error inside flatMap.
y
mg6maciej: if I replace Subject with Observable, than I get a terminal event. What's the difference?
m
What observable exactly? If you replace it with observable that emits one time and never completes, you will surely not get a terminal event in this construct.
For example
Observable.never<Unit>().startWith(Unit)
.
y
omg, you have a point!
I can’t believe I didn’t realise that, thanks!
m
No problem.
y
hey @mg6maciej , this makes me think composing
Observable
and
Completable
isn’t as trivial as I had been thinking about it. how do you recommend handling a use case like `Observable -> Completable -> Subscriber`:
userClicksObservable.flatMap { save(it).toObservable<Unit>() }.subscribe { // do something }
where save is
Completable
?
m
Depending on what you want to achieve. Anyway if
save
can fail you don't want to do this, cause userClicks will stop working after error.
y
yeah it actually could fail…
if it is, let’s say, a network request.
m
Jake Wharton did a nice presentation on this, although it's a bit too complex to my taste:

https://www.youtube.com/watch?v=0IKHxjkgop4

I'm not sure what you want to achieve yet, but something there might help you find the direction.
y
This is a great talk, I watched it once and gonna watch it again. Don’t remember him mentioning anything like chaining Observables with Completables though
this use case is quite simple, user clicks (Observable) and for each of this click the network request should fire (Completable)
this is a simplified version of what I have
m
And it doesn't?
y
it does, but because of Completable I never get
onNext
fired
in my
Subscriber
but it seems like this chain needs to be split into two parts, so that to subscribe to Completable from
onNext
m
Oh, that should be simple. Just concat it with
just(Unit)
.
Then you get
onNext
when
save
completes.
y
I’ve tried chaining
Completable
with
flatMap
emitting
just(Unit)
, but I realised there’s no items emitted to trigger it
m
Or just(Unit).startWith save.
y
No, it doesn’t work. I gonna split this chain into two different and contemplate on chaining these.
m
someCompletable.toObservable<Unit>().concatWith(just(Unit))
doesn't result in
onNext
calls?
Or
just(Unit).startWith(someCompletable.toObservable<Unit>())
, but this is the same.
y
let me see, I only tried it with
concatMap
which is a different thing
no, it doesn’t
this is the thingy I’m running val subject = PublishSubject.create<Unit>() subject.flatMap { Completable.complete().toObservable<Unit>().concatWith { just(Unit) } } .subscribe({ println(“onNext”) }, { println(“error”) }, { println(“onComplete”) }) subject.onNext(Unit)
I actually know I’ve never come across this use case, this is because all our Observables, which were written with first RxJava version, before Completables were introduced, just happen to emit only one object followed by terminate event…
m
.concatWith(just(Unit))
instead of
.concatWith { just(Unit) }
?
y
ooops
there!
m
😉
concatWith { }
is some dark magic.
y
it is!
m
.concatWith { it.onNext(Unit); it.onComplete() }
should work as well 🙂
y
that’s cool, thanks. Appending a
Completable
with
concat
is not a big price to pay compared to splitting the chain into two independent. I mean, the second one requires the same handing as threading, keeping subscriptions, etc.