I'm developing an Android app using Clean Architecture and I'm migrating it to RxJava 2.x. I have to make some network requests to a soap service, so I defined the api interface in the domain module:
public interface SiginterApi { Observable<User> login(String user, String password); ... Observable<List<Campaign>> getCampaigns(List<Long> campaignIds); }
I've read that a network request should be made with "Flowable
", because of the backpressure management since it's a 'cold observable'. On the other hand, I know the result of the request will be success (with the response) or error, so I don't know if I should use Flowable
or Single
or even Observable
.
Furthermore, I have a database accesses like this:
public interface UserRepository extends Repository { Observable<Void> saveUser(String username, String hashedPassword, boolean logged, User user); ... Observable<User> findUser(String username, String hashedPassword); }
I don't know if I should use Completable
/Flowable
/Observable
in saveUser
method and Single
/Flowable
/Observable
in findUser
method.
If there is a possibility that the consumer can be overflooded, then we use Flowable. One example could be getting a huge amount of data from a sensor. They typically push out data at a high rate. In the previous version of RxJava, this overflooding could be prevented by applying back pressure.
Observable: emit a stream elements (endlessly) Flowable: emit a stream of elements (endlessly, with backpressure) Single: emits exactly one element. Maybe: emits zero or one elements.
Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable , that have more concise API. Single represent Observable that emit single value or error. You can think of the differences like the differences of a method that returns: Collection of Objects - Observable.
Single is an Observable which only emits one item or throws an error. Single emits only one value and applying some of the operator makes no sense.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With