Skip to content Skip to sidebar Skip to footer

Async Not Waiting For Await

I'm new to Kotlin and the coroutines. However I want to use it to initialize the Android ThreeTen backport library which is a long running task. I'm using the Metalab Async/Await L

Solution 1:

The short answer is that you should not use Kotlin coroutines for that.

The long answer is that your code needs AndroidThreeTen to be initialised before you initialise your UI, so you have to wait for AndroidThreeTen.init to finish before trying to invoke initUI anyway. Because of that inherent need to wait, there is little reason to overcomplicate your code. Coroutines are not magic. They will not make waiting for something that takes a lot of time somehow faster. AndroidThreeTen.init will take the same amount of time with coroutines or without them.

You should just write your code like this:

overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val application = this

    AndroidThreeTen.init(application)
    initUI()
}

Post a Comment for "Async Not Waiting For Await"