RxJava and event streams in Android
Hello there, in this article my aim is to explain the fundamentals of RxJava and its applications towards event streams during android development. lets do a revisit on some of the popular programming paradigms we know.
- OOP: Popularly known as Object Oriented Programming sees everything as an object. Meaning an Object has a way of encapsulating its state and allow outside object to modify it through its member function.
- FP: functional Programming: More or else like a mathematical precision that helps us to describe or define what a program does.
- Rx: Reactive Extensions, this a paradigm use to capture events that uses a data flow to construct their applications. This is going to be our central focus in this article in practicing data flow. This programming paradigm has proven to be usable in production on large scale and enterprise projects. its considered scaleable for solving real life projects.
RxJava and event Streams
Rxjava has indeed helped us adopt an efficient ways of saving us from “callback hell” which usually happen when we are trying to call multiple APIs.
In this article we shall be making use of library called Retrofit from square for networking purposes. lets add the dependency by opening up our android studio.
Subscribers
when you want to respond to a change in one of your observables — a signal of some sort- you will define the function that is called with the value of an assignment. This function is called a subscriber or observer. from the context of a simple observable, its more or less like a callback function thats called every time you have a new value.
The observable from which we want to get updates (numbersOfOrangeObservable). Subscribers are sometimes called observers, the term is not frequently used because it can be confused with the word observable.
Note: An observable (the publisher)
sends a value, and a subscriber consumes it. But the trick is, you can
modify the values on the way.
RxJava 2 observable types
one of the biggest change in RxJava 2 was the introduction of several more observable types and the split between the subscriber and the observable class.
Observable
this is the most used observable type , it can emit any number of values and then complete or emit an error.
Single
This is for following special cases when you expect one value to be emitted. The Single cant complete without emitting a value. this is majorly useful for the following:
i.Network Responses
ii. Result from complex calculations
iii. to list operator that converts items of an observable into a single list
Maybe
Maybe is like Single , but with no guarantee of getting that single value.Maybe can either emit an item and complete, or just complete.
Completeable
Completeable dosen’t emit anything, but just completes or not. it basically triggers if an event happened or finished.
How to make a network request with an observable?
- Create a network observable.
Instead of a function with a callback to start the request, you can split the task into two. The first step is to create an observable and start the request.
2. Subscribe to the network observable.
we now have an observable that can give us the the data as soon as its available. we can subscribe to the observable to set the handling function.
3.Wait for a response
4. Receive data in the subscriber and do your thing.
When we have everything set, our callback will be triggered with the fresh data from the network.
How does changing the network request into an observable make our lives easier? lets look at an example.
A News Feed Aggregator
lets examine a little app that can load multiple News feed and show them in one listview, ordered by date. lets start with two simple news APIsand expand it from there.
there are two API endpoints but only one list of mixed content. We need to make teo calls and combine the results.
Google News
https://news.google.com/?output=atom
The Register Software Headlines
http://www.theregister.co.uk/software/headlines.atom
lets look at the model for the news
With these utilities, you declare the Observable<List<Model>
getFeed(String url) function that takes the feed URL and returns a
list of entries it returns, as soon as the network request is ready.
Getting the data
To get the data, you need two calls — or rather two observables. For illustration purposes we need to rename them black and white
Observable<List<Model>> blackFeedObservable =
getFeed(“https://news.google.com/?output=atom");
Observable<List<Model>> whiteFeedObservable =
getFeed(
“http://www.theregister.co.uk/software/headlines.atom")
The next thing we want to figure out is how do we combine these two observables into one and draw the list.
In addition to the two data retrieval functions, you need someone
to use the data and draw the list items. You’ll create a function
drawList(List<Model>) . Here’s the sequence followed to get the data
in RxJava:
1. Start request for both feed
2. Wait until the request are completed
3. Call Draw list with combine results.
The combine latest operator
when you have multiple observables as input that needs to be aggregated into , you can use the FRP(Functional Reactive Programming ) opearator combineLatest.
You saw how to combine two observables, or streams, into one. This is one of the most important ways of manipulating your input into
output.
Whenever you see that you have more inputs than outputs, you probably need to combine them at some point. combineLatest is the most straightforward way to do this.