Notified when lateinit var has been initialised (Kotlin)

1.5k Views Asked by At

This is a straight forward question but I cannot find an answer. Is there a way to be notified when a lateinit var has been initialised in Kotlin?

I know I can check if it has been initialised with this::coolvar.isInitialized but this is not the same.

Thank you

2

There are 2 best solutions below

2
On BEST ANSWER

lateinit var works well only in the simplest cases when uninitialized value doesn't make sense in the context of the app, such as dependency injection or late initialization in onCreate().

What you need is a property delegate with a particular behavior. Take a look at Delegates.observable :

var coolvar by Delegates.observable("initial value") { _, old, new ->
    println("coolvar has been updated")
}
0
On

If you are using Kotlin coroutines you can handle it with delay() function. for complex scenarios. not like a direct one if the value changes call this function.

Example:

private var lateinit extractFilesThread: Deferred<Unit>

private fun init() {
  GlobalScope.async {loadData()}
  GlobalScope.async {doSomeWork()}
}

private suspend fun loadData() {
  extractFilesThread = GlobalScope.async { 
    initialFilesManager.extractDataIfNeeded() 
  }
}

private suspend fun doSomeWork() {
  GlobalScope.async {
    waitExtractFilesThread()
    startThisWork()
  }
}


private suspend fun waitExtractFilesThread() {
  while (!::extractFilesThread.isInitialized) {
    delay(HALF_SECOND_IN_MILI)
  }
  extractFilesThread.await()
}

so here you want to call startThisWork() after extractFilesThread Finished and you can't use await() only if it's already initialized, so we use delay() function, It's a suspend function so it won't block other function executions.