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.