RxJava 2.x: Should I use Flowable or Single/Completable?
Backpressure occurs when an Observable
is emitting items more rapidly than an operator or subscriber can consume them.
Knowing that, Backpressure is not an issue in your case as your Observable
will emit only one item so Flowable
is not a good candidate.
So the real question is whether to use Completable
or Observable
for saveUser
and Single
or Observable
for findUser
and here as only one result is expected (success or failure) for the sake of simplicity and clarity of your API, you should definitively use Completable
/Single
otherwise it will be hard to understand that only one value will be emitted which could be misleading to your API users.
Backpressure is what you get when a source Observable
is emitting items faster than a Subscriber
can consume them. It's most often a concern with hot observables, not cold ones like your network requests.
I think you should use Completable
instead of Observable<Void>
in your saveUser
method, and use Single
for all places where you follow a request/response or input/output pattern. Observable
should be used when you actually want a continuous stream of events.
Cardinality is one way of understanding the differences between Completable, Maybe and Single:
- A
Maybe<T>
is just an Observable with cardinality 0 or 1 i.e. it represents a result which can either be present or not. - A
Single<T>
is an Observable that always returns a result i.e. a cardinality of 1. - A
Completable
can be interpreted sort of as aObservable<Void>
i.e. a cardinality of 0.
So in your case you can change the signature of the repository in this way:
Completable saveUser(...);
Single<User> findUser(...);
(I didn't mention Flowable
s which are like Observable
s with backpressure).