R
R
RobCapa2021-04-11 13:10:05
Kotlin
RobCapa, 2021-04-11 13:10:05

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())
        }
    }
}

async - underlined in red. In the console I get " Cannot access 'CommonPool': it is internal in 'kotlinx.coroutines' "

In general, I have all these functions async, launch, etc. work only inside the runBlocking {} function, I can't write outside, it underlines in red. Maybe that's the way it should be, I don't know. But in articles, it seems like, in examples they are written without runBlocking.

I had trouble accessing the kotlinx package the first time I tried to access it. Of course, in most of the articles and books that I looked at, nothing was said about this. Looking for information somehow I was able to solve this problem by fixing build.gradle. Maybe he didn't do it right and that's the problem?

Probably, I generally see coroutines from the wrong angle, but so far I don’t have a whole picture. Please briefly explain what is wrong here. Can you at least link to a sensible analysis of all this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2021-04-11
@iLLuzor

I took an example from this site, but it does not work for me

This is a very old article. API has changed since then
In general, I have all these async, launch, etc. functions. work only inside the runBlocking {} function, I can't write outside,

That's right. Suspend functions can only be called within suspend functions.
The main source of knowledge should be the official documentation . Everything is clearly explained there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question