Cleaner Code With RxJava, Coroutines Kotlin Extensions, and Helper Functions

Cleaner Code With RxJava, Coroutines Kotlin Extensions, and Helper Functions

Kotlinizing RxJava and Coroutines for Android development

In this article, I will be explaining some of the Kotlin extensions and helper functions that I wrote for RxJava and Coroutines while introducing reactive patterns to my Android applications. I will be assuming that you have some familiarity with those libraries.

For each extension, I will describe the use case, how it can be implemented without an extension, how I’d prefer it to be used, and the extension that will implement that preferred way of using it. So without further ado, let us begin.

Scenario 1. Room With RxJava

Let’s say you have a Room query that returns a Flowable; this is a typical use case that enables us to observe the database and act accordingly whenever an update occurs. What happens when we want to invoke that Room query only once without observing it? We can of course always create another query with a different return type, and that’s definitely the way to go in some cases, but if we still want to take advantage of RxJava in that case, we can write a simple extension for that.

The extension ideally would return a single invocation result on success and would allow for failure handling. The signature of that extension would look like this:

fun <T> Flowable<T>.getValue(
  onSuccess: ((value: T) -> Unit),
  onFailure: ((throwable: Throwable) -> Unit)? = null
)

Database Single Invocation Signature

If we take a look at the Flowable class, you’ll notice there is a function called blockingFirst() which returns the first item emitted. It will throw a NoSuchElementExceptionif it emits no items and will throw a RuntimeExceptionif the source signals an error.

We can utilize this information to write the following extension function:

fun <T> Flowable<T>.getValue(
  onSuccess: ((value: T) -> Unit),
  onFailure: ((throwable: Throwable) -> Unit)? = null
) {
  try {
    onSuccess(blockingFirst())
  } catch (noSuchElementE: NoSuchElementException) {
    // Feel free to implement something else for this case
    onFailure?.invoke(noSuchElementE)
  } catch (exception: Exception) {
    onFailure?.invoke(exception)
  }
}

Database Single Invocation Extension Function

kotlin android

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

How to Develop an Android App with Kotlin?

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top Kotlin Android App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top-notch Kotlin App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Kotlin Coroutines on Android - How to use Coroutines on Android

Coroutines are a Kotlin feature that convert async callbacks for long-running tasks, such as database or network access, into sequential code. This Kotlin Coroutines tutorial will show you how to use coroutines on Android, and how the new androidx-concurrent library makes it easy to use them to get things off the main thread. You'll also learn how the new library helps coroutines work with Architecture Components. This session also covers coroutine patterns, best practices, and even how to test coroutines!