I have a coroutine I'd like to fire up at android startup during the splash page. I'd like to wait for the data to come back before I start the next activity. What is the best way to do this? Currently our android is using experimental coroutines 0.26.0...can't change this just yet.
UPDATED: We are now using the latest coroutines and no longer experimental
onResume() {
loadData()
}
fun loadData() = GlobalScope.launch {
val job = GlobalScope.async {
startLibraryCall()
}
// TODO await on success
job.await()
startActivity(startnewIntent)
}
fun startLibraryCall() {
val thirdPartyLib() = ThirdPartyLibrary()
thirdPartyLib.setOnDataListener() {
///psuedocode for success/ fail listeners
onSuccess -> ///TODO return data
onFail -> /// TODO return other data
}
}
The first point is that I would change your loadData function into a suspending function instead of using
launch. It's better to have the option to define at call site how you want to proceed with the execution. For example when implementing a test you may want to call your coroutine inside arunBlocking. You should also implement structured concurrency properly instead of relying onGlobalScope.On the other side of the problem I would implement an extension function on the
ThirdPartyLibrarythat turns its async calls into a suspending function. This way you will ensure that the calling coroutine actually waits for the Library call to have some value in it.Since we made
loadDataa suspending function we can now ensure that it will only start the new activity when theThirdPartyLibrarycall finishes.