'What's the proper way to use Coroutines in Activity?
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 {
}
Solution 1:[1]
For activities you should use lifecycleScope
Read more about it in the official documentation:
Solution 2:[2]
ViewModelScope by default run on main thread viewModelScope launch.
For Global Scope, read this answer which perfectly explain when to use Global scope launch.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivo Beckers |
| Solution 2 | Ankit |
