Skip to content

Exploring RxKotlin

Devrath edited this page Dec 28, 2023 · 1 revision

RxKotlin

What is RxKotlin?

  • Origins of RxKotlin: RxKotlin is derived from Rx, where Rx stands for Reactive Extensions. It is based on a set of standard principles applicable to various programming platforms. Reactive programming principles provide a standardized approach, making it easier to write and maintain code across different platforms.

  • Rx Principles: Rx is centered around reactive programming principles, offering a standard methodology across multiple programming environments. This consistency aids in organizing code concisely, enhancing readability, and simplifying maintenance.

RxJava, RxKotlin, RxAndroid

  • RxKotlin and RxJava: RxKotlin serves as the Kotlin bindings for RxJava. Kotlin, being interoperable with Java, allows direct utilization of RxJava within Kotlin code. RxKotlin acts as a layer on top of RxJava, providing lightweight extension functions that facilitate seamless communication with Kotlin.

  • RxAndroid: RxAndroid, on the other hand, serves as the RxJava bindings specifically tailored for Android development. It enables the application of reactive programming principles in Android applications.

  • Interoperability: Due to the interoperability between Java and Kotlin, developers can seamlessly integrate RxJava into Kotlin code, making use of RxKotlin for additional Kotlin-specific functionalities.

RxKotlin

  • Asynchronous Programming: RxKotlin is an asynchronous programming library centered around the concept of observables. Observables represent sequences of data, which could include continuous button clicks, data from a server, or any other stream of events over time.

  • Observables: Observables, in the context of RxKotlin, are sequences of data or events. These could be responses to user interactions, server communications, or any other time-dependent data.

Mantra

  • Everything is a Sequence: The fundamental principle of RxKotlin is that everything is a sequence or something that reacts to a sequence. This underscores the central role of sequences in the reactive programming paradigm.

Asynchronicity types

  • Button Taps
  • Keyboard Animations
  • Downloading data
  • Processing events
  • Writing to disc
  • Playing the audio and video

Android API's to achieve Asynchronicity

  • Threads
  • Runnables
  • AsyncTask
  • Coroutines
  • WorkManager

Why use RxKotlin

  • We have so many APIs in Android to deal with asynchronicity
  • RxKotlin makes it a simpler solution to manage async things instead of relying on multiple APIs
  • RxKotlin is reactive.
  • It contains a wide variety of patterns and operators.
  • The operators used to comply with other Rx standards used in other platforms.
  • RxKotlin helps to manage any possible side effects by properly managing the mutable state.
  • It helps to write code in a compositional way so that it is easier to reasonably explain the code by revisiting it compared to the traditional approach.
  • RxKotlin supports code isolation
  • RxKotlin supports decoupling
  • RxKotlin enables reusability
  • Rx concept is the multiplatform meaning we can apply skills for another platform similarly and helps to reason with other developers of other platform

When not to use RxKotlin

  • It has a steep learning curve to learn all the aspects of the programming using the reactive.
  • People who follow the regular traditional approach of programming need to practice & understand the reactive way of coding.
  • If you have a tight deadline, It is not the proper time to incorporate learning rxKotlin to your project.
  • We need a couple of dependencies to be added to the project to work with it like rxjava, rxandroid, and rxextension. It might be small but still, it's quite a couple of dependencies that we use.
  • We need not have to use rxkotlin when all other developers in the project are not interested in learning rxkotlin also because they have to understand your code right.
  • If you use it without understanding, you can get into complications.

Pattern used for RxKotlin

  • Observable & Observer also sometimes known as Publisher & Subscriber
  • We create a sequence of observables and reactively observe them.
  • An observable won't emit them until there are one or more subscribers.

Life cycle of an observable

  • Successful Cycle
    • Here is a sequence of events is triggered one after another
    • On each emission onNext event is triggered
    • If there is not interruption dure to error, Completed event is triggered
  • Un-Successful Cycle
    • Here is a sequence of events are triggered one after another
    • On each emission onNext event is triggered
    • If there is a interruption, now OnError event is triggered, the OnNext event is not triggered after the error is found, Finally OnCompletion is not triggered.
Clone this wiki locally