rx-java2
Is there something like Single.empty()
Single.empty() makes no sense because Single has to have a single item or an error. You could just have kept Observable or switched to Maybe which does allow empty or Completable which doesn’t emit an item at all.
Difference between doAfterTerminate and doFinally
The difference is that doFinally executes the provided Action if the downstream cancels/disposes the sequence in addition to the regular onError or onComplete termination paths. This allows cleaning up and releasing resources by all three means. The operator also guarantees that the action gets executed exactly once per subscription even in case if the onError … Read more
RxJava flatMapIterable with a Single
flattenAsObservable should do the trick, it will map Single success value to Iterable (list), and emit each item of the list as an Observable. getListOfItems() .flattenAsObservable(new Function<Object, Iterable<?>>() { @Override public Iterable<?> apply(@NonNull Object o) throws Exception { return toItems(o); } }) .flatMap(item -> doSomethingWithItem()) .toList()
How to convert rxJava2’s Observable to Completable?
The fluent way is to use Observable.ignoreElements(). Observable.just(1, 2, 3) .ignoreElements() Convert it back via toObservable if needed.
How to reset a BehaviorSubject
I assume you want to clear the BehaviorSubject (because otherwise don’t call onComplete on it). That is not supported but you can achieve a similar effect by having a current value that is ignored by consumers: public static final Object EMPTY = new Object(); BehaviorSubject<Object> subject = BehaviorSubject.createDefault(EMPTY); Observable<YourType> obs = subject.filter(v -> v != … Read more
Do I have to unsubscribe from completed observable?
Yes you are correct. After a stream is terminated ( onComplete / onError has been called ), subscriber unsubscribes automatically. You should be able to test these behaviors using isUnsubscribed() method on the Subscription object.
RxJava 2.x: Should I use Flowable or Single/Completable?
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 … Read more
RxJava Single.just() vs Single.fromCallable()?
Usually you will notice the difference when the thing you’re emitting is not just an object but actually a result of some method calls that involve either heavy computation, I/O, or state. Single.just(x) evaluates the x immediately in the current thread and then you’re left with whatever was the result of x, for all subscribers. … Read more