I have an Android service which starts and syncs different types of data with the server when it's online. I'm new to Kotlin coroutines and I'm trying to accomplish the following:
fun syncData{
//Job1 make retrofit call to server
//Job2 make retrofit call to server after job1 is done.
//Job3 make retrofit call to server after job2 is done and so on.
//After all jobs are done I'll stop service.
}
I'm following this post: Kotlin Coroutines the right way in Android
Which brought me to this solution:
fun syncData() = async(CommonPool){
try{
val sync1 = async(CommonPool){
job1.sync()
}
val sync2 = async(CommonPool){
job2.sync()
}
val sync3 = async(CommonPool){
job3.sync()
}
val sync4 = async(CommonPool){
job4.sync()
}
job1.await()
job2.await()
job3.await()
job4.await()
}catch (e: Exception){
}finally {
stopSelf()
}
}
But when I get retrofit's log on logcat, every call is mixed. Calls from job3 comes before job1, and so on. How can I execute them in a pipeline? I'm kinda lost in Kotlin's coroutines so I don't know how exactly to implement this.