4

For viewModels we can use

 viewModelScope.launch {
     //long running operation
 }

this is the proper way for viewModel.

Same like this, what's the best way to launch coroutines in Activity?

I used GlobalScope launch in Activity, is it right approach?

GlobalScope.launch {

}
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

2 Answers2

12

For activities you should use lifecycleScope

Read more about it in the official documentation:

Use Kotlin coroutines with lifecycle-aware components

Ivo
  • 18,659
  • 2
  • 23
  • 35
1

ViewModelScope by default run on main thread viewModelScope launch.

For Global Scope, read this answer which perfectly explain when to use Global scope launch.

Ankit
  • 335
  • 1
  • 10