I have a simple sequence of suspend functions (in Android Kotlin) which I want to run in sequence (once first is finished, then run second.)
I tried doing something which seamed reasonable like this:
class SomeService {
init {
GlobalScope.launch(Dispatchers.IO) {
firstSuspendFunction()
secondSuspendFunction()
}
}
}
However, only first one is run, second is never executed. Why is that so?
Edit: I tried to rule out some possible issues, and what seemed to worked was by completely emptying first function (empty body)!
The problem is that my firstFunction actually works, it has something like this:
private suspend fun firstSuspendFunction() {
localProtoDataStore.preferences.collect {
someDataList.addAll(it.preferenceData)
}
}
Could it be that somehow launch never knowns if first suspend function has finished?
firstSuspendFunction(). (It could also be running forever instead of failing -- e.g. if your flow doesn't terminate.)