https://kotlinlang.org logo
Title
j

Jabez Magomere

02/05/2020, 6:43 AM
Is it okay for a Service in android to extend Coroutine Scope?
t

tseisel

02/05/2020, 7:41 AM
Yes :
class MyService : Service(), CoroutineScope by MainScope()
But you could also use composition :
class MyService : Service() {
    private val scope = MainScope()
}
In either case, don't forget to cancel the scope in onDestroy.
l

louiscad

02/05/2020, 8:30 AM
Just extend
LifecycleService
from AndroidX Lifecycle Service and use
lifecycleScope
.
2
m

Manuel Vivo

02/05/2020, 10:02 AM
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

voben

02/05/2020, 4:05 PM
When would we cancel the created scope? In the service
onDestroy
method?
:yes: 4