David
David

Reputation: 387

Kotlin coroutine not stopping when Android app terminates

It seems I don't understand Kotlin coroutine scope like I thought I did. I was under the impression that a coroutine was automatically canceled when the scope ended. I start an infinitely looped coroutine conditioned with 'isActive.':

    override fun onCreate(savedInstanceState: Bundle?)
    {
        GlobalScope.launch {
            while (isActive)
            {
                log(this, object {}, "Hello GlobalScope", false)
                delay(500)
            }
        }
        ...

Since it uses the global scope I expected it to stop when the app is terminated but instead it keeps running after the app is terminated:

I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope
I/BluetoothLeash: StandaloneCoroutine.invokeSuspend(): Hello GlobalScope

What am I misunderstanding?

Upvotes: 1

Views: 3201

Answers (2)

Manuel Mato
Manuel Mato

Reputation: 779

Try to use lifecycleScope.launch { } The documentation said:

  • This scope will be cancelled when the [Lifecycle] is destroyed.

Upvotes: 2

Pranav Choudhary
Pranav Choudhary

Reputation: 2796

If you take a look at the definition of GlobalScope you will find that it is declared as an object:

object GlobalScope : CoroutineScope { ... }

And an object represents a static instance. So it will consume some memory while your app is running. And even if your app is terminated but the process is not destroyed, a launched coroutine in GlobalScope may still be running.

GlobalScope.launch(Dispatchers.IO): Launches a top-level coroutine on Dispatchers.Default. This coroutine is unbound and keeps running until the task is finished or cancelled

Use GlobalScope may be a bad idea because it is not bound to any job.

Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely.

Upvotes: 2

Related Questions