Kotlin coroutine scope definition

527 Views Asked by At

Imagine that I have a coroutine scope called CryptographyScope:

object CryptographyScope : CoroutineScope {
     override val coroutineContext: CoroutineContext =
        Dispatchers.IO + CoroutineName("CryptographyScope")
}

So, in many places in my application I call CryptographyScope.async.

CryptographyScope.async {
    cryptographyService.decrypt(value) 
} 
  • What happens when one of the cryptographyService.decrypt(value) fails and throws an exception? Does it cancel every coroutine that uses CryptographyScope in the application at the moment of execution?

  • Should the CryptographyScope be a singleton?

2

There are 2 best solutions below

0
Chih On BEST ANSWER

CoroutineScope defines a scope where you contain, delimit and keep track of all concurrent operations and tying them to the lifecycle of your application entity.

I was going to call decrypt through the custom scope I had created CryptographyScope. But, this isn't right since I don't have a any entity with defined lifecycle so won't be able to avoid leak happening.

The correct thing to do is:

fun decryptAll() = coroutineScope {
    async { 
        cryptographyService.decrypt(value1) 
    }
    async { 
        cryptographyService.decrypt(value2) 
    }
}
0
Vishvnath pratap singh On

GlobalScope.launch {} starts a new coroutine in the 'global' scope and launch {} starts a new coroutine in a CoroutineScope.

eg.

    fun main() {
        println("1. Let's start")
        runBlocking {
            launch { 
                delay(1000)
                println("3. coroutine ONE")
            }

            launch { 
                delay(500)
                println("2. coroutine TWO")
            }
        }

    println("4. Only when the children inside runBlocking complete, execution follows on this line")
}

in the above snippet, the line at 4. will only execute when both coroutines defined inside runBlocking {} have been completed.

Now let try running the same code with GlobalScope.launch {}:

fun main() {
    println("1. with GlobalScope.launch {}")
    runBlocking {
        GlobalScope.launch {
            delay(1000)
            println("3. coroutine ONE ")
        }

        GlobalScope.launch {
            delay(100)
            println("2. coroutine TWO")
        }
    }

    println("4. This line will execute even if the coroutines inside runBlocking did not complete.")
}

in above snippet 4. This line will execute even if the coroutines inside runBlocking did not complete.

However, the coroutines launched in the above example are running in a separate 'global' scope, where runBlocking has no control over.