Reactive programming has become very popular in last time. I think for beginner developer it could be a little bit overwhelming and difficult to understand. Why should you direct your attention to this approach?
Firstly, what is reactive programming and RxJava or RxAndroid? The most popular definition is that reactive programming is an extension of the Observer software design pattern to support sequences of data and/or events. It works in an asynchronous way. Simply reactive programming consist of data streams and the propagation of change. And what is RxJava? This is Reactive Extensions for the JVM (Java Virtual Machine) that provides the easy way to handle asynchronous operations and manages concurrency. RxAndroid? Yes, of course, this is the extension for Android. The main advantage is the easy threading and loopers managing.
The fundamental question is why? Imagine, you have to get preferences from a user and local database, request to the cloud database, show a message before starting the connection and after the finish, and finally, do something. In Android it is complicated, you need to use a few AsyncTasks, create something as a semaphore, store result, handle errors and do something after completed all. RxJava allows doing all of this processes in a simple way places everything as a flow in one place by that increasing readability and in easy way control concurrency. This is a power of reactive programming. Your background tasks are executed and after all, you can react for example update your UI. Maybe it will be a simple task, but maybe it will be the combination of few and progress will be visible. Additionally, an amount of code could be decreased by using lambdas.
And how to use it?
RxJava introduces observable types, observer, subscriptions and methods for modifying, formatting, composing and transforming the data stream.
Let’s take a short look for each one.
Observable is an object that can emit data. There are a few types: Flowable, Observable, Single, Maybe, Completable.
Above is the example of Observable. Observable emits objects by onNext() method, after finishing run onComplete() and if error occurs onError() is called.
An Observer is an object that waits and watches the Observable and reacts whenever the Observable publishes results. To make it possible, Observable have to be subscribed with Observer. Following example shows how it works. Of course, subscriptions could be realized by other methods.
And operators that allow manipulating data stream. There is a pretty long list of available operators. They could be divided into categories:
- Creating Observables – Create, Defer, Empty/Never/Throw, From, Interval, Just, Range, Repeat, Start, Timer
- Transforming Observables – Buffer, FlatMap, Map, GroupBy, Scan, Window
- Filtering Observables – Debounce, Distinct, ElementAt, Filter, First, IgnoreElements, Last, Sample, Skip, SkipLast, Take, TakeLast
- Combining Observables – And/Then/When, CombineLatest, Join, Merge, StartWith, Switch, Zip
- Error Handling Operators – Catch, Retry
- Observable Utility Operators – Delay, Do, Materialize/Dematerialize, ObserveOn, Serialize, Subscribe, SubscribeOn, TimeInterval, Timeout, Timestamp, Using
- Conditional and Boolean Operators – All, Amb, Contains, DefualtIfEmpty, SequenceEqual, SkipUntil, SkipWhile, TakeUnitl, TakeWhile
- Mathematical and Aggregate Operators – Average, Concat, Count, Max, Min, Reduce, Sum
- Backpressure Operators – strategies if Observables produces items more rapidly than their observers consume them
- Connectable Observable Operators – Connect, Publish, Replay, RefCount
- Operators to Convert Observables – To
Summarizing, In this article, I presented the main idea of this concept. It is the long, sometimes difficult topic, but worth your time.