Re: Avoid Backing Properties for LiveData and StateFlow

Re: Avoid Backing Properties for LiveData and StateFlow

Recently I’ve stumbled upon this article by Danny Preussler and it got me thinking, if by utilising Kotlin features there isn’t any even better way of avoiding backing fields.

Recently I’ve stumbled upon this article by Danny Preussler and it got me thinking, if by utilising Kotlin features there isn’t any even better way of avoiding backing fields.

I’ve come up with one idea and while I don’t think backing fields are so evil that they need to be avoided at all cost, it was interesting for me to play around with this topic a little. And while I don’t necessarily recommend to use this way of avoiding backing fields, it might be interesting to showcase this idea to present some lesser known tricks in Kotlin.

My immediate thought after reading the article was, whether there isn’t any way of utilising Delegate pattern in Kotlin to avoid backing fields. I was inspired by mutableStateOf() in Jetpack Compose, which enables in ViewModel this kind of syntax:

var someState by mutableStateOf(1)
  private set

This is great, because it enables changing state only within the ViewModel and the view layer has no option of modifying the state, only to see the latest value.

Can we leverage this approach for LiveData<T> or StateFlow<T> to avoid having to write backing field?

Turns out this is not so easy after all. To achieve this kind of functionality, Jetpack compose uses extension functions on its own custom types MutableState<T> and State<T> like this:

@Suppress("NOTHING_TO_INLINE")
inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value

@Suppress("NOTHING_TO_INLINE")
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
    this.value = value
}

Note: I didn’t know you could overwrite setter and getter in this way in Kotlin! This is a nice lesson learned by skimming through the Jetpack Compose codebase.

kotlin clean-code 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!