Transformation using RxJava tutorial for beginners

Published by inkskull on

Transformation using RxJava tutorial for beginners

Checkout the previous post here

Suppose I want to append my signature to the “Hello, world!” output. One possibility would be to change the Observable:

Observable.just("Hello, world! -Dan")
    .subscribe(s -> System.out.println(s));

This works if you have control over your Observable, but there’s no guarantee that will be the case – what if you’re using someone else’s library? Another potential problem: what if I use my Observable in multiple places but only sometimes want to add the signature?

How about we try modifying our Subscriber instead:

Observable.just("Hello, world!")
    .subscribe(s -> System.out.println(s + " -Dan"));

This answer is also unsatisfactory, but for different reasons: I want my Subscribers to be as lightweight as possible because I might be running them on the main thread. On a more conceptual level, Subscribers are supposed to be the thing that reacts, not the thing that mutates.

Wouldn’t it be cool if I could transform “Hello, world!” with some intermediary step?


Categories: RxJava2


Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By :