Global Coroutine scope is scary

Global Coroutine scope is scary

Overview Using suspend methods with coroutines is a new norm while using Kotlin in Android, Kotlin Native or Kotlin Multiplatform. Recently, I have been searching good libraries for the multiplatform for a pet project I am working on, and I have seen many libraries and implementations that uses GlobalScope as coroutine scope. This is a huge antipattern because using GlobalScope can lead to memory leaks. Problem with GlobalScope Though in Android, using suspend methods is fairly easy because you can just use viewModelScope because most of the heavy lifting is being done inside viewmodels. So, No need to switch to CoroutineScope. But occasionally, there is a need for asynchronous work outside ViewModel. A common example of it would be an external SDK in your codebase may require an instance of a class performing module specific or app related tasks. ...

January 29, 2022 · 3 min · SlothieSmooth