6

There are different ways of launching a coroutine in Kotlin. I found a couple of examples where GlobalScope and CoroutineScope are used. But the latter one is being created directly when launching a coroutine:

  1. Using GlobalScope:

    fun loadConfiguration() {
        GlobalScope.launch(Dispatchers.Main) {
           val config = fetchConfigFromServer() // network request
           updateConfiguration(config)
        }
    }
    
  2. Using CoroutineScope instances, created directly when launching a coroutine:

    fun loadConfiguration() {
        CoroutineScope(Dispatchers.Main).launch {
            val config = fetchConfigFromServer() // network request
            updateConfiguration(config)
        }
    }
    

In this case is there a difference between these two approaches?

Doesn't the second case violate the principle of structured concurrency?

Sergio
  • 27,326
  • 8
  • 128
  • 149
  • have you checked https://stackoverflow.com/questions/65008486/globalscope-vs-coroutinescope-vs-lifecyclescope/65047468#65047468? – ADM Jan 05 '22 at 12:53
  • @ADM, thanks for the link, it seems this is it. – Sergio Jan 05 '22 at 13:17
  • 3
    Yes, the second one is an anti-pattern. There's no reason to create a new scope if you aren't going to store a reference to it or manage its lifecycle. And using GlobalScope in some cases is a code smell, at least often enough that it now shows a compiler warning by default if you use it at all. I think they created a monster of newbies overusing it since they use it in all their basic examples. – Tenfour04 Jan 05 '22 at 14:05

1 Answers1

4

Doesn't the second case violate the principle of structured concurrency?

Actually both cases violate it equally, because they have pretty much the exact same semantics. All the warnings against using GlobalScope.launch that you may have encountered, apply to CoroutineScope().launch just the same. A slight difference is that the latter is entirely pointless, whereas the former at least has some legitimate use cases.

Writing CoroutineScope().launch instead of GlobalScope.launch obscures the intent and consequences, as well as creates a needless object every time.

I think the CoroutineScope().launch idiom came into being solely because it circumvented the warnings you get for GlobalScope.launch.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436