Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning RxJava
  • Table Of Contents Toc
Learning RxJava

Learning RxJava - Second Edition

By : Nick Samoylov, Nield
4.8 (4)
close
close
Learning RxJava

Learning RxJava

4.8 (4)
By: Nick Samoylov, Nield

Overview of this book

RxJava is not just a popular library for building asynchronous and event-based applications; it also enables you to create a cleaner and more readable code base. In this book, you’ll cover the core fundamentals of reactive programming and learn how to design and implement reactive libraries and applications. Learning RxJava will help you understand how reactive programming works and guide you in writing your first example in reactive code. You’ll get to grips with the workings of Observable and Subscriber, and see how they are used in different contexts using real-world use cases. The book will also take you through multicasting and caching to help prevent redundant work with multiple Observers. You’ll then learn how to create your own RxJava operators by reusing reactive logic. As you advance, you’ll explore effective tools and libraries to test and debug RxJava code. Finally, you’ll delve into RxAndroid extensions and use Kotlin features to streamline your Android apps. By the end of this book, you'll become proficient in writing reactive code in Java and Kotlin to build concurrent applications, including Android applications.
Table of Contents (22 chapters)
close
close
1
Section 1: Foundations of Reactive Programming in Java
5
Section 2: Reactive Operators
12
Section 3: Integration of RxJava applications
1
Appendix A: Introducing Lambda Expressions
2
Appendix B: Functional Types
5
Appendix E: Understanding Schedulers

Setting up

Currently, there are three co-existing versions of RxJava: 1.x, 2.x, and 3.0. We will go through some of the major differences later in the section entitled RxJava 1.x, 2.x, 3.0 – which one do I use? and discuss which version you should use.

RxJava 3.0 is a fairly lightweight library and comes in at fewer than 4 megabytes (MBs) in size. This makes it practical for Android and other projects that require a low dependency overhead. RxJava 3.0 has only one dependency, called Reactive Streams ( http://www.reactive-streams.org/), which is a core library (made by the creators of RxJava) that sets a standard for asynchronous stream implementations, one of which is RxJava 3.0.

RxJava 2x is even smaller—closer to 2 MB—and has only one dependency on Reactive Streams too.

It may be used in other libraries beyond RxJava and is a critical effort in the standardization of reactive programming on the Java platform. Note that RxJava 1.x does not have any dependencies, including Reactive Streams, which was realized after 1.0.

If you are starting a project from scratch, try to use RxJava 3.0. This is the version we will cover in this book, but we will point out significant differences between versions 1.x and 2.x. While RxJava 1.x and 2.x will be supported for a good while due to the countless projects using it, innovation will likely only continue onward in RxJava 3.0. RxJava 1.x reached end-of-life on March 31, 2018, and RxJava 2.x will only be maintained by fixing bugs until February 28, 2021.

All RxJava versions can run on Java 1.6+. In this book, we will use Java 8, and it is recommended that you use a minimum of Java 8 so that you can use lambdas out of the box. For Android, there are ways to leverage lambdas in earlier Java versions that will be addressed later. But due to the fact that Android Nougat uses Java 8 and Java 8 has been out since 2014, we hope that you will not have to do any workarounds to leverage lambdas.

Navigating the central repository

To bring in RxJava as a dependency, you have a number of options. The best place to start is to go to the Maven central repository, called The Central Repository (http://search.maven.org) and search for rxjava. You should see RxJava 3.0, 2.x, and 1.x as separate repositories at the top of the search results, as shown in the following screenshot:

You can also use classic search, if you're already used to its look and feel, and get the same results as shown in the following screenshot:

At the time of writing, RxJava 3.0.0 is the latest version of RxJava 3.x, RxJava 2.2.17 is the latest version of RxJava 2.x, and RxJava 1.3.8 is the latest version of RxJava 1.x. You can download the latest JAR file by clicking the link on the far right under the Download column and then configuring your project using the downloaded JAR file.

Alternatively, you can use Gradle or Maven to automatically import these libraries into your project. This way, you can easily share and store your project (through Git or other version control systems) without having to download and configure RxJava manually each time. To view the latest configurations for Maven, Gradle, and several other build automation systems, click on the Latest Version link and copy the dependency description provided into the pom.xml file (for Maven) or build.gradle file (for Gradle) of your project.

In the next two subsections, we will walk you through how to do it.

Using Gradle

There are several automated build systems available, but the two most popular ones are Gradle and Maven. Gradle is somewhat of a successor to Maven and used mostly for Android development. If you are not familiar with Gradle and would like to learn how to use it, check out the Gradle Getting Started guide (https://gradle.org/getting-started-gradle-java/).

There are also several books that cover Gradle in varying degrees of depth that you can find at https://gradle.org/books/. The following is the fragment of the above screenshot that contains Maven and Gradle configurations:

In your build.gradle script, ensure that you have declared mavenCentral() as one of your repositories. Type in or paste this dependency line, compile 'io.reactivex.rxjava2:rxjava:x.y.z', where x.y.z is the version number you want to use, as shown in the following code snippet:

apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'io.reactivex.rxjava2:rxjava:x.y.z'
}

Build your Gradle project and you should be good to go! You will then have RxJava and its types available for use in your project.

Using Maven

You also have the option to use Maven, and you can view the appropriate configuration in The Central Repository by selecting the Apache Maven configuration information, as shown in the following screenshot:

You can then copy and paste the <dependency> block containing the RxJava configuration and paste it inside a <dependencies> block in your pom.xml file. Rebuild your project, and you should now have RxJava set up as a dependency. The x.y.z version number corresponds to the desired RxJava version that you want to use:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.nield</groupId>
<artifactId>mavenrxtest</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>x.y.z</version>
</dependency>
</dependencies>
</project>
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning RxJava
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon