O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PublishSubject

One of the most straightforward Subject instance, is PublishSubject. The way PublishSubject works is that it just relays all the elements received to its subscribers. It can work as a message bus between different peers.

That's explained best by an example. Consider the following code:

Subject<Long> subject = PublishSubject.create();Observable.interval(2, TimeUnit.SECONDS)        .take(5)        .doOnSubscribe((d) -> log("Original-doOnSubscribe"))        .doOnComplete(() -> log("Original-doOnComplete"))        .subscribe(subject);subject        .doOnSubscribe((d) -> log("First-doOnSubscribe"))        .doOnComplete(() -> log("First-doOnComplete"))        .subscribe(v -> log("First:" + v));Thread.sleep(4100);subject        .doOnSubscribe((d) -> log("Second-doOnSubscribe"))        .doOnComplete ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required