Is it okay for a Service in android to extend Coro...
# coroutines
j
Is it okay for a Service in android to extend Coroutine Scope?
t
Yes :
Copy code
class MyService : Service(), CoroutineScope by MainScope()
But you could also use composition :
Copy code
class MyService : Service() {
    private val scope = MainScope()
}
In either case, don't forget to cancel the scope in onDestroy.
l
Just extend
LifecycleService
from AndroidX Lifecycle Service and use
lifecycleScope
.
2
m
Don’t make it implement CoroutineScope. Either create a new scope within it or use the
lifecycleScope
. A service should have a scope, not be a scope
5
v
When would we cancel the created scope? In the service
onDestroy
method?
👌 4