简体   繁体   中英

Kotlin coroutine does not run synchronously

In all cases that I have been using corrutines, so far, it has been executing its "lines" synchronously, so that I have been able to use the result of a variable in the next line of code.

I have the ImageRepository class that calls the server, gets a list of images, and once obtained, creates a json with the images and related information.

class ImageRepository {

    val API_IMAGES = "https://api.MY_API_IMAGES"

    suspend fun fetch (activity: AppCompatActivity) {

        activity.lifecycleScope.launch() {

            val imagesResponse = withContext(Dispatchers.IO) {
                getRequest(API_IMAGES)
            }

            if (imagesResponse != null) {
                val jsonWithImagesAndInfo = composeJsonWithImagesAndInfo(imagesResponse)

            } else {
                // TODO Warning to user
                Log.e(TAG, "Error: Get request returned no response")
            }

          ...// All the rest of code
        }
    }
}

Well, the suspend function executes correctly synchronously, it first makes the call to the server in the getRequest and, when there is response, then composes the JSON. So far, so good.

And this is the call to the "ImageRepository" suspension function from my main activity:

lifecycleScope.launch {
    val result = withContext(Dispatchers.IO) { neoRepository.fetch(this@MainActivity) }
    Log.i(TAG, "After suspend fun")
}

The problem is that, as soon as it is executed, it calls the suspension function and then displays the log, obviously empty. It doesn't wait for the suspension function to finish and then display the log.

Why? What am I doing wrong?

I have tried the different Dispatchers, etc, but without success.

I appreciate any help.

Thanks and best regards.

It's because you are launching another coroutine in parallel from inside your suspend function. Instead of launching another coroutine there, call the contents of that launch directly in your suspend function.

A suspend function is just like a regular function, it executes one instruction after another. The only difference is that it can be suspended, meaning the runtime environment can decide to halt / suspend execution to do other work and then resume execution later.

This is true unless you start an asynchronous operation which you should not be doing. Your fetch operation should look like:

class ImageRepository {

    suspend fun fetch () {
        val imagesResponse = getRequest(API_IMAGES)

        if (imagesResponse != null) {
            val jsonWithImagesAndInfo = composeJsonWithImagesAndInfo(imagesResponse)
        } else {
            // TODO Warning to user
            Log.e(TAG, "Error: Get request returned no response")
        }
        ... // All the rest of code
    }

}

-> just like a regular function. Of course you need to all it from a coroutine:

lifecycleScope.launch {
    val result = withContext(Dispatchers.IO) { neoRepository.fetch() }
    Log.i(TAG, "After suspend fun")
}

Google recommends to inject the dispatcher into the lower level classes ( https://developer.android.com/kotlin/coroutines/coroutines-best-practices ) so ideally you'd do:

val neoRepository = ImageRepository(Dispatchers.IO)

lifecycleScope.launch {
    val result = neoRepository.fetch()
    Log.i(TAG, "After suspend fun")
}

class ImageRepository(private val dispatcher: Dispatcher) {

    suspend fun fetch () = withContext(dispatcher) {
        val imagesResponse = getRequest(API_IMAGES)

        if (imagesResponse != null) {
            val jsonWithImagesAndInfo = composeJsonWithImagesAndInfo(imagesResponse)
        } else {
            // TODO Warning to user
            Log.e(TAG, "Error: Get request returned no response")
        }
        ... // All the rest of code
    }

}

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