Rx in Android Overview

Tram Ho

Hello everyone, today I would like to share about the topic of Asynchronous Programming. Specifically, a library is quite popular in asynchronous processing, which helps to optimize the processing of tasks, especially heavy tasks. Here, I am referring to the RxJava library.

I. Reactive Programming

First I have to talk about the term Reactive Programming, the ideological foundation to create RxJava. So what is Reactive Programming?

Reactive Programming is a programming method that focuses on asynchronous data streams and observes the changes of those asynchronous data streams, when there is a change, appropriate action will be taken. Since this is an asynchronous data flow, the code components run at the same time on different threads thereby shortening the execution time without blocking the main thread.

Popular libraries in Reactive Programming on Android include RxJava and RxAndroid, which are developed based on Microsoft’s Reactive Extensions (Rx).

Reactive Extension (ReactiveX or RX) is a library that follows the rules of Reactive Programming i.e. it composes asynchronous and event-based programs using observable strings.

Reactive Extension is available in many languages ​​like C++ (RxCpp), C# ( Rx.NET ), Java (RxJava), Kotlin (RxKotlin) Swift (RxSwift), …

Benefits of using Reactive Programming in Android include:

  • Quick response : Helps the application respond quickly to events and changes in the data flow.
  • Ease of management : Data streams are managed explicitly and operators can be used to transform and manipulate data easily.
  • Readable and maintainable code : Reactive Programming often uses explicit math operations and data processing sequences, making the code easier to read, understand, and maintain.
  • Good integration : Reactive Programming can integrate with different libraries and technologies in data processing, such as internet, databases and asynchronous tasks.

II. RxJava/Rx Kotlin

RxJava is basically a library that provides asynchronous events developed based on the Observer Pattern. RxJava allows you to create and manage data streams (observable streams) and perform transformations, filtering, combining and handling events, receiving data values ​​in these streams through the Observer. In particular, you can coordinate and process them on any Thread you want.

Benefits of using RxJava in Android include:

  • Easy asynchronous handling : RxJava makes handling asynchronous tasks easy and neat, avoiding the use of cumbersome and complicated callbacks.
  • Data Flow Management : RxJava allows for explicit data flow management and handling, resulting in code that is easier to read and understand.
  • Good integration with other components : RxJava has good integration with other components in Android such as LiveData, ViewModel, Retrofit and Room, making it possible to build robust and easy-to-maintain Android applications.

Rx Kotlin is a collection of RxJava extension methods for Kotlin. There will be methods that make it easier for you to create reactive programming code, convert, combine data generation types, …

III. RxAndroid

RxAndroid is an extension of RxJava, optimized and comes with specific support features for Android application development. It provides additional tools and capabilities to conveniently use RxJava in the Android environment.

RxAndroid helps to interact with the user interface (UI) while using RxJava. It provides helper classes for reactive programming in Android, including:

  • Schedulers : Provides optimized Schedulers for Reactive Programming in Android. Helps us to coordinate, divide, and optimize activities in different Threads. For example MainThreadScheduler to execute tasks on the main thread (UI thread).
  • AndroidObservable : Provides helper methods for creating Observables from Android components such as UI, touch events, system notifications, and GPS location.
  • Binding APIs : Supports RxJava integration with popular UI libraries like Data Binding and ButterKnife, making it easy and flexible to handle data in UI components.

There is one small thing you can keep in mind while implementing the RxJava/RxAndroid library set-up for Project Android that you can just declare Rx Android in the app’s build.gradle file. App will still run normally because Rx Android will then automatically pull Rx Java back. But often the Rx Java version here is the old version, less updated because it depends on Rx Android and it is also rarely updated.

So I have a recommendation here that when declaring using RxAndroid, you should declare both RxJava / Kotlin to always be up to date.

IV. Main components in RxJava

To create RxJava, basically consists of 2 most important components including Observable and Observer. In addition, there are a number of components that play a role in deploying, coordinating, manipulating data, and connecting, … thereby optimizing the execution of tasks using RxJava.

Below is a diagram illustrating an implementation of 1 RxJava, demonstrating Observables and their transformations.

image

  1. Observable : Represents a data stream that emits events or data values ​​over time. An Observable is a data source and is capable of emitting events and values ​​from that source.
  2. Observer : An object that receives and processes events or values ​​from an Observable. The Observer will register to receive notifications from the Observable and define actions to handle when an event or value is emitted.
  3. Operators : Are the transformations and data processing on Observables to create new data streams or perform processing tasks. Operators allow you to transform, filter, combine, group data, and perform calculations on data in streams.
  4. Scheduler : Used to specify the flow of events and tasks in RxJava. The scheduler determines whether processing should take place on the main thread (UI thread) or the background thread.
  5. Disposable : Represents the Observer’s subscription and unsubscribe to the Observable. Disposable allows you to manage the subscription lifecycle and release resources when they are no longer needed.
  6. Subject : An Observable that is also an Observer, allowing you to broadcast and receive events and data values ​​like a regular Observable. Subject can be used to create interaction between Observables and Observers.

I will introduce each of these components, specifically what it is, how to create and deploy them in the next post

Share the news now

Source : Viblo