Rxjava: 2.x doIfEmpty for Maybe

Created on 28 May 2017  路  10Comments  路  Source: ReactiveX/RxJava

Hi,

I wanted to simply log something when a Maybe is empty.
I could not find a proper side-effect method like doIfEmpty(Action).
Am I missing something or this by purpose?

2.x Question

All 10 comments

What about doOnComplete?

Isn't doOnComplete also called when the Maybe has an item? I want something executed only when the Maybe is empty.

No that's onSuccess

With Maybe, the signal types are mutually exclusive thus onComplete indicates an empty Maybe and doOnComplete will execute for empty Maybes.

Thx for clarification.

As a sidenote: I think the Maybe type is somewhat inconsistent in method naming.
To me, the terms _empty_ and _complete_ are used arbitrary.
The protocol uses onComplete to signal an empty Maybe.
Why is it then Maybe.empty() and not Maybe.complete()?
Why is it then switchIfEmpty and not switchOnComplete?
Why is it then defaultIfEmpty and not defaultIfComplete?

I think it would be more consistend and more readable if Maybe would use the term _empty_ all over the place. So the protocol would be onSuccess|onEmpty|onError.
Even if one would not consult the javadocs(as I did now), the meaning of onEmpty would be self-explanatory to most of us.

It is due to historical and symmetry reasons: many operators evolved in other types and have been adapted to other while keeping the naming to enable better discoverability. You have to look at the whole picture, not just a type in isolation.

The lucky thing is, now that Kotlin is mainstream on Android as well, you can just use Kotlin extension methods with the same convenience as any other RxJava methods and name those methods as you see fit.

I would suggest to improve documentation of Maybe's doOnComplete() and doOnSuccess() to explicitly mention when each one is called. Currently even a marble diagram for doOnComplete() pictures a Maybe emitting a few items and then completing.

I came here to create a similar issue, good thing I searched first :) So I guess that proves that current documentation is a bit misleading in this regard... Maybe a separate issue should be created for this.

It would be great if you could go through the Maybe JavaDoc and create a checklist issue about which diagrams are copy-paste or missing.

Ok, will try to do that!

Hi guys, i stumbled over the same issue and it costs me two days :D it would be nice if you could provide a convenient method for this case. i didnt knew that onComplete will only be called when Maybe emits 0 results..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

midnight-wonderer picture midnight-wonderer  路  3Comments

philleonard picture philleonard  路  3Comments

yubaokang picture yubaokang  路  3Comments

ZakTaccardi picture ZakTaccardi  路  3Comments

hoc081098 picture hoc081098  路  3Comments