K Merle
10/17/2021, 3:48 PMviewModelScope
while closing the screen. Network request gets canceled as coroutine scope is canceled. How do I send a network request upon closing the screen?streetsofboston
10/17/2021, 3:58 PMK Merle
10/17/2021, 4:06 PMCoroutineScope
and run network request, does scope cancels itself after all suspend functions are called?Francesc
10/17/2021, 4:16 PMwithContext(NonCancellable) {...}
K Merle
10/17/2021, 4:53 PMprivate var job: Job? = null
job = CoroutineScope(IO).launch {
networkRequest()
job!!.cancel()
}
streetsofboston
10/17/2021, 5:49 PMK Merle
10/17/2021, 5:51 PMstreetsofboston
10/17/2021, 5:52 PMK Merle
10/17/2021, 5:52 PMstreetsofboston
10/17/2021, 5:53 PMK Merle
10/17/2021, 5:54 PMwithContext(NonCancellable)
inside viewModelScope?streetsofboston
10/17/2021, 5:54 PMK Merle
10/17/2021, 5:55 PMstreetsofboston
10/17/2021, 5:56 PMK Merle
10/17/2021, 6:06 PMWhenever you need some work to run beyond its current scope, we recommend creating a custom scope in your Application class and running coroutines within it. Avoid using GlobalScope, ProcessLifecycleOwner scope and NonCancellable for this type of work.
I might go for a singleton CoroutineScope in the end.