Reactive Programing Fundamentals(Rx Kotlin)

What is Reactive Programming?

Reactive Programming is programming with asynchronous data streams. … RX) is an implementation of the reactive programming principles to “compose asynchronous and event-based programs by using observable sequence”. With RX, your code creates and subscribes to data streams named Observables.

It not a programing language its a just approach using that we can apply to any language like RxJava, RxKotlin, RxJS, RxSwift

Rx Kotlin
Rx Kotlin

Where we can use Reactive Programming in Android?

So as an android developer where we can use reactive programming in our application. There are lots of places where we can use reactive programming in android applications.

e.g When we want to handle asynchronous data in our android application like making API call, DB operations, Observing click events, validation there are lots more things we can do using reactive programming.

What are the advantages of Rx kotlin ?

  1. Simple callback using we can handle operation is an easy manner
  2. We can easily perform CRUD operation on data using operator from the Rx
  3. Multithreading operation makes easy
  4. Less BoilerPlate Code

E.g If we want modified the list on backend thread so we see both approach using rxapproach and oldApproach
val listoFData= mutableListOf<String>()
listoFData.add(“CR7”)
listoFData.add(“MESSI”)
listoFData.add(“ZALTAN”)
listoFData.add(“NEYMAR”)

private fun rxApproach() {
// Itreation , multithreading , modifying data done in one place Observable.just(listoFData)
.map { it+”FootballPlayer” }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
// Got the result
},
{

}
)} // End of fun

private fun oldApproach() {
// for itreation
for(item in listoFData){
System.out.print(item)
}
// for background opeation we have crete seprate therad
// Out append operation we have write some more code
}

Finally how we can use Rx in our app?

Rx is just a library so we have to add in the Gradle file

// dependencies for reactive stuffs
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.14'
implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.1'

So on the next blog, we see the Main aspect used in the Rx kotlin

Happy Coding! from r@wh0st

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store