RxJava: How to convert List of objects to List of another objects

If your Observable emits a List, you can use these operators: flatMapIterable (transform your list to an Observable of items) map (transform your item to another item) toList operators (transform a completed Observable to a Observable which emit a list of items from the completed Observable) Observable<SourceObjet> source = … source.flatMapIterable(list -> list) .map(item -> … Read more

RxJava delay for each item of list emitted

The simplest way to do this seems to be just using concatMap and wrapping each item in a delayed Obserable. long startTime = System.currentTimeMillis(); Observable.range(1, 5) .concatMap(i-> Observable.just(i).delay(50, TimeUnit.MILLISECONDS)) .doOnNext(i-> System.out.println( “Item: ” + i + “, Time: ” + (System.currentTimeMillis() – startTime) +”ms”)) .toCompletable().await(); Prints: Item: 1, Time: 51ms Item: 2, Time: 101ms Item: … Read more

Difference between RxJava API and the Java 9 Flow API

What are the main differences between these two libraries? The Java 9 Flow API is not a standalone library but a component of the Java Standard Edition library and consists of 4 interfaces adopted from the Reactive Streams specification established in early 2015. In theory, it’s inclusion can enable in-JDK specific usages, such as the … Read more

What is the purpose of doOnNext(…) in RxJava

doOnNext is for side-effects: you want to react (eg. log) to item emissions in an intermediate step of your stream, for example before the stream is filtered, for transverse behavior like logging, but you still want the value to propagate down the stream. onNext is more final, it consumes the value.

Android RxJava 2 JUnit test – getMainLooper in android.os.Looper not mocked RuntimeException

This error occurs because the default scheduler returned by AndroidSchedulers.mainThread() is an instance of LooperScheduler and relies on Android dependencies that are not available in JUnit tests. We can avoid this issue by initializing RxAndroidPlugins with a different Scheduler before the tests are run. You can do this inside of a @BeforeClass method like so: … Read more

How to use CompositeDisposable of RxJava 2?

private final CompositeDisposable disposables = new CompositeDisposable(); // adding an Observable to the disposable disposables.add(sampleObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<String>() { @Override public void onComplete() { } @Override public void onError(Throwable e) { } @Override public void onNext(String value) { } })); static Observable<String> sampleObservable() { return Observable.defer(new Callable<ObservableSource<? extends String>>() { @Override public ObservableSource<? extends … Read more

How to chain two Completable in RxJava2

You are looking for andThen operator. Returns a Completable that first runs this Completable and then the other completable. firstCompletable .andThen(secondCompletable) In general, this operator is a “replacement” for a flatMap on Completable: Completable andThen(CompletableSource next) <T> Maybe<T> andThen(MaybeSource<T> next) <T> Observable<T> andThen(ObservableSource<T> next) <T> Flowable<T> andThen(Publisher<T> next) <T> Single<T> andThen(SingleSource<T> next)

Retrofit with Rxjava Schedulers.newThread() vs Schedulers.io()

You are correct that the benefit of using Schedulers.io() lies in the fact that it uses a thread pool, whereas Schedulers.newThread() does not. The primary reason that you should consider using thread pools is that they maintain a number of pre-created threads that are idle and waiting for work. This means that when you have … Read more

In RxJava, how to pass a variable along when chaining observables?

The advice I got from the Couchbase forum is to use nested observables: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1) … .flatmap( (data2) -> { // I can access data1 here return op2(data2); }) }); EDIT: I’ll mark this as the accepted answer as it seems to be the most recommended. If your processing … Read more