简体   繁体   中英

Coroutines : Runblocking freeze Android UI

I'm developing an android app in Kotlin. After validation of an entry form, I launch 3 jobs to do 3 http calls in the same time:

val jobA = CoroutineScope(IO).launch {
   // saves some data in my database
}
val jobB = CoroutineScope(IO).launch {
   // saves an image in my aws bucket
} 
val jobC = CoroutineScope(IO).launch {
   // if exists, deletes the old image in my aws bucket
} 

Before launching theses 3 jobs, I start a loader animation (I'm using this library: https://github.com/81813780/AVLoadingIndicatorView )

I need to wait completion of my 3 jobs without freezing UI (or at least the animation).

I tried with runBlocking but it freezes the UI...

runBlocking() {
   jobA!!.join()
   jobB!!.join()
   jobC!!.join()
}

How can I wait for my 3 jobs without freezing UI?

Thanks in advance, Sorry for my english, Thomas

You can try to launch another coroutine using Main CorotineContext and wait for jobs there:

CoroutineScope(Main).launch {
    jobA!!.join()
    jobB!!.join()
    jobC!!.join()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM