Understanding Coroutine Scopes in Android :
GlobalScope vs. ViewModelScope vs. LifecycleScope vs. rememberCoroutineScope:

Murat Aydın
Loodos
3 min readApr 3, 2023

--

When working with coroutines in Android, it’s important to understand the different scopes available to you. In this article, we’ll explore four of the most commonly used scopes: GlobalScope, ViewModelScope, LifecycleScope, and rememberCoroutineScope. We’ll examine the differences between these scopes and provide code examples to illustrate their usage.

GlobalScope
The GlobalScope is a top-level scope that can be used to launch coroutines that have a global lifetime. Coroutines launched in this scope will continue to execute until the application is terminated or until they are explicitly cancelled. The GlobalScope should be used sparingly, as it can easily lead to memory leaks and other issues.

Here’s an example of how to use GlobalScope to launch a coroutine that logs a message every second:

GlobalScope.launch {
while (true) {
Log.d("Coroutine Example", "Logging a message every second")
delay(1000)
}
}

ViewModelScope
The ViewModelScope is a scope that is tied to a specific ViewModel. Coroutines launched in this scope will automatically be cancelled when the ViewModel is destroyed. This makes it a good choice for performing asynchronous operations that are tied to the lifecycle of a ViewModel.

Here’s an example of how to use ViewModelScope to launch a coroutine that retrieves data from a repository:

class MyViewModel(private val repository: MyRepository): ViewModel() {
fun fetchData() {
viewModelScope.launch {
val data = repository.getData()
// Update UI with data
}
}
}

LifecycleScope
The LifecycleScope is a scope that is tied to the lifecycle of a specific Android component, such as an Activity or Fragment. Coroutines launched in this scope will automatically be cancelled when the component is destroyed. This makes it a good choice for performing asynchronous operations that are tied to the lifecycle of a component.

Here’s an example of how to use LifecycleScope to launch a coroutine that retrieves data from a repository:

class MyActivity : AppCompatActivity() {
private val viewModel: MyViewModel by viewModels()
private val scope = lifecycleScope

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

scope.launch {
val data = viewModel.getData()
// Update UI with data
}

scope.launchWhenCreated {
val data = viewModel.getData()
// Update UI with data
// launchWhenCreated will launch coroutine if the lifecycle is at least on create state and gets suspended if it's on destroy state.
}

scope.launchWhenStarted {
// Perform operations when the Activity is in the Started state
// launchWhenStarted will launch coroutine if the lifecycle is at least on start state and gets suspended if it's on stop state.
}
scope.launchWhenResumed {
// Perform operations when the Activity is in the Resumed state
// launchWhenResumed will launch coroutine if the lifecycle is at least on resume state and gets suspended if it's on pause state.
}
}
}

rememberCoroutineScope

The rememberCoroutineScope is a scope that can be used in composable functions in Jetpack Compose. Coroutines launched in this scope will automatically be cancelled when the composable function is removed from the composition. This makes it a good choice for performing asynchronous operations that are tied to the lifecycle of a composable function.

Here’s an example of how to use rememberCoroutineScope to launch a coroutine that retrieves data from a repository in a composable function:

@Composable
fun MyComposableFunction(viewModel: MyViewModel) {
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
val data = viewModel.getData()
// Update UI with data
}
}

Conclusion
In this article, we’ve explored the differences between GlobalScope, ViewModelScope, LifecycleScope, and rememberCoroutineScope. Each scope has its own advantages and disadvantages, and choosing the right one for your use case is important. By understanding the differences between these scopes and how to use them, you’ll be able to write better, more efficient coroutine code in your Android app.

--

--