Skip to content

Checked Lazy

mainThreadLazy that checks property access on main thread, and checkedLazy to make your own variant.

Supported platforms: Android.

This library provides a checkedLazy() method that returns a Lazy delegate, as well as mainThreadLazy().

checkedLazy() takes as first parameter a function where you can implement an access check.

The second parameter is the lazy initializer, as in Kotlin stdlib lazy.

mainThreadLazy { … } is a shorthand for checkedLazy(::checkMainThread) { … }. It’s there because main thread checking is a common use case on Android due to its synchronized nature and its omnipresence.

Example

val noMainThreadChecker = noAccessOn(mainThread)

class YourClass {
    val greeting: String by mainThreadLazy { "Hello Splitties!" }
    val expensiveObject by checkedLazy(noMainThreadChecker) { doHeavyInstantiation() }
}

Download

implementation("com.louiscad.splitties:splitties-checkedlazy:3.0.0-beta01")