Do all scopes get cancelled on it's own, when the ...
# coroutines
p
Do all scopes get cancelled on it's own, when the application is terminated? I have a scope, that has basically same lifetime as the application. I'm wondering if I should bother closing it. I guess it will get canceled, on JVM shutdown or something?
s
When an application is terminated, then there's nothing left. Everything dies with it. Or do you mean if CoroutineScopes get signalled on app termination signals, just before the app terminates (so you can close resources, for example)? I don't think so. Everything just dies with the app.
e
it depends on the dispatcher
the worker threads created to run tasks for the default dispatchers (Default and IO) set isDaemon = true so they do not block application shutdown
if you create one with ExecutorService.asCoroutineDispatcher() then it depends on the executor
Executors.newFixedThreadPool
etc. use
Executors.defaultThreadFactory()
unless you specifically specify a thread factory, and the default thread factory sets
isDaemon = false
, so they do block application shutdown
(forever, until the dispatcher is closed/cancelled)
p
Hm, I see. Thank you. Do you maybe know, how does this translate to Android? I wonder how long would the system allow to keep it running.
e
depends on the system configuration, how much memory pressure it's under, and possibly other device-specific constraints
r
For background anything on newer Androids, you'll need a Service to register with Android that you're allowed to run in the background.
👍 1
e
* reliably on any Android version, your app might be running in the background for a while, but it's undetermined how long