Answer the question
In order to leave comments, you need to log in
Kotlin Coroutines - can't figure it out?
(You don't have to read this part)
{ The second day I'm trying to make at least some progress in the study of coroutines, but in a complete stupor. All materials on them are simply disgusting (at least what I found in Russian). Here they only talk about the general essence, and here is an example with constructions that have not been explained by anyone. You try to run them at home - so they still do not work. There is no consistency in the material, each article is some kind of hodgepodge. Some knowledge of multithreading in Java does not help at all. In general, I apologize for the extra lines of text, it hurt. }
I took an example from this site , but it doesn't work for me:
import android.annotation.SuppressLint
import kotlinx.coroutines.*
class Test() {
companion object {
@SuppressLint("NewApi")
@JvmStatic
fun main(args: Array<String>) = runBlocking(CommonPool) {
sendEmailSuspending()
println("Email sent successfully.")
println("Finished")
}
suspend fun sendEmailSuspending(): Boolean {
val msg = async(CommonPool) {
delay(500)
"The message content"
}
val recipient = async(CommonPool) { getReceiverAddressFromDatabase() }
println("Waiting for email data")
return sendEmail(recipient.await(), msg.await())
}
}
}
Answer the question
In order to leave comments, you need to log in
I took an example from this site, but it does not work for me
In general, I have all these async, launch, etc. functions. work only inside the runBlocking {} function, I can't write outside,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question