Skip to content

This is a starter project for demonstrating simple RxJava2 samples in Android.

License

Notifications You must be signed in to change notification settings

JacquesSmuts/RxStarter

Repository files navigation

CircleCI ktlint

RxStarter

This is a starter project for demonstrating RxJava2. I gave a presentation on this repo, available on YouTube.

Get it on Google Play

Getting into RxJava

Work your way through these links to get into RxJava.

Link Reason to Read
Observer Pattern Background on Observer pattern and why a naive implementation can easily result in memory leaks. Luckily RxJava avoids this by design.
ReactiveX Intro RX's official intro to Reactive Programming. You've probably seen this and it feels lacking.
Functional Reactive An article which will explain the differences in mindset between reactive and imperative programming
Froussios Intro to RxJava A very definitive intro to RxJava basics. Must Read.
Froussios Intro to RxJava Seriously. This is the one. Read it.
Android Samples Samples of RxJava in Android. Ugly due to lack of Lambdas, but extensive examples.
RxJava repo This is the official repo but the documentation here is actually not that great for starting out. Don't feel bad if this doens't make a lot of sense.

Usage

The app has a number of samples that you can play around with in the app. There are shorthand explanations on each screen plus more detailed information in the code comments.

Each activity has a Java and Kotlin equivalent. The Kotlin classes are not just conversions of the Java classes. The Kotlin versions contain additional nice things such as Kotlin Extension functions.

The below tables lists the places in the code where each RxJava function is called. Observable Creation shows how you can go about creating an Observable that emits items.

Observable Creation Description Example
RxView.clicks() Creates an Observable out of clicks on any View Most of them. ButtonRapidClickActivity
.just() Create an Observable/Single from a function which returns a result. The function is fired once and the result retained. Easily used as substitute for Async. SingleActivity
.fromCallable() Create an Observable/Single from a function which returns a result. The function is fired only after subscribing, and again with initial subscriptions. Easily used as substitute for Async. LongApiCallActivity
.interval() Create an observable which emits an object every x milliseconds LongApiCallActivity, DistinctActivity
.merge() Merge two or more observables into one. Useful for running various events through the same filter. ThreadSwitchActivity
.mergeWith() Same as merge, just a different syntax. PinActivity
PublishSubject.create() Create an Observable you can send events to and subscribe to. ListActivity, ThreadSwitchActivity
publisher.hide() Turn a PublishSubject such as the one above into an observable that you can only listen/subscribe to. ListActivity

The Rx Operators Table list operators that can be applied to an rxJava chain.

Rx Operators Description Example
.subscribe() final action to take for each event. Can contain onNext(), onError() and/or onComplete() All of them
.map() Turn a given input into something else. Most of them. ButtonRapidClickActivity
.observeOn() Switches between threads, such as background and UI thread. Most of them, but especially ThreadSwitchActivity
.subscribeOn() Switches the initial Observable to a specified thread. LongApiCallActivity
.dispose() Clean up a subscription for Garbage Collection. Essential for preventing memory leaks. LongApiCallActivity, BaseActivity
.throttleFirst() Filters out any subsequent emissions past the first one in a given timeframe. ButtonRapidClickActivity
.scan() Keeps a single object which can be changed by each subsequent emission. Usually to keep a running tally. ButtonRapidClickActivity
.distinct() Filters out any emission which has occurred in the past. DistinctActivity
.distinct() Filters out any emission which has occurred in the past. DistinctActivity
.buffer() Keep emissions from reaching downstream until a given number or time has been reached. Useful for batching items. PinActivity
.donOnNext() Do something on each emission which doesn't affect the stream. Useful for side-effects or for doing something regardless of whether the end-result is filtered out or not. PinActivity
.doOnError() If there is any error, determines what should be done before reaching .subscribe() as an error FailingApiActivity

About

This is a starter project for demonstrating simple RxJava2 samples in Android.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published