I am very excited with new RxJava Sources
such as: Single
, Maybe
, Completable
, which make your interfaces classes cleaner and prevent from a lot of mistakes during create of your 'Source' (e.g. forgetting to call onComplete()
)
But it requires lots of boilerplate to combine them into a complex stream.
E.g. we have common Android situation of loading and caching data. Let's assume we have 2 sources api
and cache
and we would like to combine it:
public interface Api {
Single<Integer> loadFromNetwork();
}
public interface Cache {
Maybe<Integer> loadFromCache(); //maybe because cache might not have item.
}
let's try to combine it:
final Single<Integer> result = cache.loadFromCache()
.switchIfEmpty(api.loadFromNetwork());
it will not compile, because Maybe
doesn't have overload Maybe.switchIfEmpty(Single):Single
so we have to convert everything:
final Single<Integer> result = cache.loadFromCache()
.switchIfEmpty(api.loadFromNetwork().toMaybe())
.toSingle();
Another possible way to combine it also requires сonversion:
final Single<Integer> result = Observable.concat(
cache.loadFromCache().toObservable(),
api.loadFromNetwork().toObservable()
).firstOrError();
So I don’t see any way to use the new sources without many transformations that add code noise and create a lot of extra objects.
Due to such issues, I can't use Single
, Maybe
, Completable
and continue to use Observable
everywhere.
So my question is:
What are the best practices of combining
Single
,Maybe
,Completable
.Why these Sources don't have overloads to make combing easier.
Why these Sources don't have common ancestor and use it as
parameter ofswitchIfEmpty
and other methods?
P.S. Does anybody know why these classes doesn't have any common hierarchy?
From my perspective if some code can work for example with Completable
it will also works fine with Single
and Maybe
?
switchIfEmptySingle
perhaps you could ask for it. – TremblesFlowable
, this stream will not emit if there isn't anything specific to your query. At the same time you send a request out and if there is data returned and when you put it into your table and it's different theFlowable
will emit. – Cellobiose