kind of android related, but which coroutine scope...
# coroutines
j
kind of android related, but which coroutine scope would you use in background services? e.g. intent services or job services?
e
As long as it’s not reading files or anything I’d use
Dispatchers.Default
j
that's the context but how would you launch them?
s
Copy code
val scope = CoroutineScope(Dispatcthers.Default + SuperVisorJob())

scope.launch { .... }
👍 1
j
thanks 🙂
a
CoroutineWorker
with WorkManager
3
just creating a new scope like the above doesn't include when to cancel it for cleanup or otherwise manage lifecycle along with Android; CoroutineWorker does this for you
and WorkManager is a lot more compatible and robust across Android versions than working with IntentService or JobService directly
2
s
Agreed with @Adam Powell with above implementation you will have to manage your coroutineScope to cancel. Better use WorkManager where you can return results as well.
d
Just beware of unique work, for some reason it could get stuck... if you return failed for sure, but even if not, sometimes I have some works that stay blocked and just don't run until all works get purged.
Whereas IntentService just queued them, and keeps running them even if previous ones fail.