launch function returns a Job. You can keep a reference to it and check its isActive property, but that is not how well-architected UI applications are usually written. Instead, you typically have some observable property (or LiveData, or Rx subject, or conflated broadcast channel, or whatever else you are using in your UI models to track ongoing asynchronous operations) and write something like:

loadingData = true
launch {
try {
doLoadData()
} finally {
loadingData = false
}
}

A larger application would usually define this loadingData property in some base class of its view models (or whatever else — depending on the architecture) and provide some convenience function to set and reset it.

Written by

Project Lead for the Kotlin Programming Language @JetBrains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store