Exploring RxJava in Android — Introduction
We must all have heard about the Reactive Programming principles when developing android applications. While there are multiple resources written on how to get started in RxJava and RxAndroid, I found it difficult to keep track of everything in one place. This article is just to highlight the basics of various components of RxJava, while also providing some examples on how this would be applicable to Android development.
This article is part of RxJava Introduction series. You can checkout the entire series here:
- RxJava Basics and Operators for creating Observables — You are here
- RxJava Operators: Operators for Transforming Observables
- RxJava Operators: Operators for Filtering Observables
- RxJava Operators: Operators for Combining Observables
- RxJava Operators: Utility Operators
- RxJava Operators: Conditional and Boolean Operators
- RxJava Operators: Mathematical and Aggregate Operators
- RxJava: Different types of Observables
- RxJava: Different types of Subjects
Reactive Programming
So let’s begin by providing a definition of Reactive Programming:
Reactive Programming is a programming paradigm oriented around data flows and the propagation of change i.e. it is all about responding to value changes. For example, let’s say we define x = y+z. When we change the value of y or z, the value of x automatically changes. This can be done by observing the values of y and z.
Reactive Extensions is a library that follows Reactive Programming principles to compose asynchronous and event-based programs by using observable sequence.
RxJava is a Java based implementation of Reactive Programming.
RxAndroid is specific to Android platform which utilises some classes on top of the RxJava library.
RxJava Basics
The building blocks of RxJava are:
- Observable: class that emits a stream of data or events. i.e. a class that can be used to perform some action, and publish the result.
- Observer: class that receivers the events or data and acts upon it. i.e. a class that waits and watches the Observable, and reacts whenever the Observable publishes results.
The Observer has 4 interface methods to know the different states of the Observable.
- onSubscribe(): This method is invoked when the Observer is subscribed to the Observable.
- onNext(): This method is called when a new item is emitted from the Observable.
- onError(): This method is called when an error occurs and the emission of data is not successfully completed.
- onComplete(): This method is called when the Observable has successfully completed emitting all items.
RxJava Operators
Now that we have implemented a basic Observable with an Observer, we can take a look at the different operators in RxJava. Operators allow you to manipulate the data that was emitted or create new Observables.
Operators for creating Observables
Create
This operator creates an Observable from scratch by calling observer methods programmatically. An emitter is provided through which we can call the respective interface methods when needed.
Sample Implementation: The below sample creates an Observable using Observable.create()
method. The create()
method does not have an option to pass values. So we have to create the list beforehand and perform operations on the list inside the onNext()
method. The below code will print each item from the list.
Output:
onSubscribe
onNext: A
onNext: B
onNext: C
onNext: D
onNext: E
onNext: F
onComplete
Defer
This operator does not create the Observable until the Observer subscribes. The only downside to defer()
is that it creates a new Observable each time you get a new Observer. create()
can use the same function for each subscriber, so it’s more efficient.
Sample Implementation: The below sample creates an Observable using Observable.defer()
method. The below code creates an Observable that emits a value.
From
This operator creates an Observable from set of items using an Iterable, which means we can pass a list or an array of items to the Observable and each item is emitted one at a time. Some of the examples of the operators include fromCallable(), fromFuture(), fromIterable(), fromPublisher(), fromArray()
.
Sample Implementation: The below sample creates an Observable using Observable.from()
method. The below code will print each item from the array one by one. The order is also preserved.
Output:
onNext: A
onNext: B
onNext: C
onNext: D
onNext: E
onNext: F
Interval
This operator creates an Observable that emits a sequence of integers spaced by a particular time interval.
Sample Implementation: The below sample creates an Observable using Observable.interval()
method. The below code will print values from 0 after every second.
Output:
onNext: 0
onNext: 1
onNext: 2
onNext: 3
onNext: 4
onNext: 5
onNext: 6
onNext: 7
onNext: 8
onNext: 9
Just
This operator takes a list of arguments (maximum 10) and converts the items into Observable items. just()
makes only 1 emission. For instance, If an array is passed as a parameter to the just()
method, the array is emitted as single item instead of individual numbers. Note that if you pass null
to just()
, it will return an Observable that emits null
as an item.
Sample Implementation: The below sample creates an Observable using Observable.just()
method. The below code will print the entire list in a single emission.
Output:
onNext: [A, B, C, D, E, F]
Difference between Observable.from()
and Observable.just()
— For the same input, if you see the above code, Observable.just()
emits only once whereas Observable.from()
emits n times i.e. the length of the array, in this case 6.
Range
This operator creates an Observable that emits a range of sequential integers. The function takes two arguments: the starting number and length.
Sample Implementation: The below sample creates an Observable using Observable.range()
method. The below has a starting number of 2 and a range of 5 numbers, so it will print values from 2 to 6.
Output:
onNext: 2
onNext: 3
onNext: 4
onNext: 5
onNext: 6
Repeat
This operator creates an Observable that emits a particular item or sequence of items repeatedly. There is an option to pass the number of repetitions that can take place as well.
Sample Implementation: The below sample creates an Observable using Observable.repeat()
method. The below code will print the same values as the previous range() operator but since the repeat is specified as 2, the same values will be printed twice.
Output:
onNext: 2
onNext: 3
onNext: 4
onNext: 5
onNext: 6
onNext: 2
onNext: 3
onNext: 4
onNext: 5
onNext: 6
Timer
This operator creates an Observable that emits one particular item after a span of time that you specify.
Sample Implementation: The below sample creates an Observable using Observable.timer()
method. The below code will emit only once after a 1 second delay.
Output:
onNext: 0
Difference between Observable.interval()
and Observable.timer()
— timer()
emits just a single item after a delay whereas interval()
operator, on the other hand, will emit items spaced out with a given interval.
That’s it guys! This is part one of the series on RxJava. I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.
Happy coding!