Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava 2.x: Should I use Flowable or Single/Completable?

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.

like image 467
Pablo Alonso González Avatar asked Mar 01 '17 07:03

Pablo Alonso González


People also ask

What is the purpose of a flowable in RxJava?

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.

What is difference between flowable and Observable?

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.

What is the difference between Observable and single?

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.

What is single rxjava2?

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.


2 Answers

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.

like image 136
npace Avatar answered Sep 18 '22 18:09

npace


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.

like image 34
Nicolas Filotto Avatar answered Sep 19 '22 18:09

Nicolas Filotto