https://kotlinlang.org logo
#android
Title
# android
d

dave08

11/21/2019, 1:09 PM
Is there some kind of coroutine scope already handled by Android Lifecycle components for Services and IntentServices?
a

Adam Powell

11/21/2019, 2:13 PM
What kinds of services are you working with? You mention IntentService, have you looked at WorkManager?
For WM there's a
CoroutineWorker
available that plays quite nicely with them
d

dave08

11/21/2019, 2:16 PM
I've seen it, but I have a SyncAdapter that needs to run a whole chain of things, and I was hoping to save the refactoring for later 🙃... so if everything is going towards
WorkManager
then `Service`s are only for building chains of work items and launching them?
And they apply also for SyncAdapters? The only problem is that there won't really be any way to report back to the SyncAdapter if there were any errors...
That's really part of the point of SyncAdapters, they run themselves again with some backoff type logic.
d

dekans

11/21/2019, 2:25 PM
There is
LifecycleService
class you can extend instead of
Service
This is part of androidx.lifecyle library
d

dave08

11/21/2019, 2:30 PM
Is there any CoroutineScope that comes with that @dekans?
d

dekans

11/21/2019, 2:34 PM
lifecycleScope
will come with it, as it's a
LifcyleOwner
🙂
👍🏼 1
d

dave08

11/21/2019, 2:35 PM
Is there something similar for
IntentService
?
Then again, it might be easy to mimic one using a buffered
Flow
, no?
d

dekans

11/21/2019, 2:44 PM
I dont think so, but in this case, implementation is pretty easy. Check the
LifecycleService
source
2 Views