RxJava and parallel execution of observer code

RxJava is often misunderstood when it comes to the asynchronous/multithreaded aspects of it. The coding of multithreaded operations is simple, but understanding the abstraction is another thing. A common question about RxJava is how to achieve parallelization, or emitting multiple items concurrently from an Observable. Of course, this definition breaks the Observable Contract which states … Read more

RxJava – Just vs From

The difference should be clearer when you look at the behaviour of each when you pass it an Iterable (for example a List): Observable.just(someList) will give you 1 emission – a List. Observable.from(someList) will give you N emissions – each item in the list. The ability to pass multiple values to just is a convenience … Read more

Kotlin quadruple, quintuple, etc. for destructuring

Basics Let’s see how destructuring works: Kotlin defines a convention for this, i.e. componentX() operator functions are an example of the principle of conventions used in Kotlin in many places. These componentX() functions are used by the compiler for the initialization of variables in destructuring declarations. For example in Pair<A,B> these functions look as follows: … Read more

How to call a method after a delay in android using rxjava?

This is how I would do it: Completable.timer(5, TimeUnit.SECONDS, AndroidSchedulers.mainThread()) .subscribe(this::getTransactionDetails); A Completable represents a deferred computation with no value but an indication for completion or exception. The static method call timer() returns a Completable that signals completion after the specified time period has elapsed, and the subscribe() call will mean that the method getTransactionDetails() … Read more

RxJava: chaining observables

Sure, RxJava supports .map which does this. From the RxJava Wiki: Basically, it’d be: loginObservable() .switchMap( someData -> fetchUserDataObservable(someData) ) .map( userData -> cacheUserData(userData) ) .subscribe(new Subscriber<YourResult>() { @Override public void onCompleted() { // observable stream has ended – no more logins possible } @Override public void onError(Throwable e) { // do something } @Override … Read more

How to stop and resume Observable.interval emiting ticks

Here’s one possible solution: class TickHandler { private AtomicLong lastTick = new AtomicLong(0L); private Subscription subscription; void resume() { System.out.println(“resumed”); subscription = Observable.interval(5, TimeUnit.SECONDS, Schedulers.io()) .map(tick -> lastTick.getAndIncrement()) .subscribe(tick -> System.out.println(“tick = ” + tick)); } void stop() { if (subscription != null && !subscription.isUnsubscribed()) { System.out.println(“stopped”); subscription.unsubscribe(); } } }