I've used JobScheduler
for a while now. A typical example would be:
JobScheduler
triggers the onStartJob()
method. This kicks off some sort of task that might include several other background processes (getting device position, making a network call, etc). I then use an interface to call back to the JobService
once the task is complete or if it fails.
However, with WorkManager
it seems like there is basically no way to run work asynchronously. I know WorkManager
will run Workers in a separate thread but it seems like a single worker must run synchronously? If so I'll have to rework a lot of the logic in my app it seems unless I'm missing something.
Say I had a weather app and it would use the JobScheduler
to run a JobService
that did the following:
onStartJob()
--> get device position --> make API request for local weather data -> write to database -> call jobFinished()
With WorkManager
, is the intended implementation of something like this to chain multiple workers together? And if so, how do you pass data from one Worker to another? Again, seems like it would be a massive undertaking for any slightly complex app to migrate to the WorkManager
API.
I think what you are looking for is a ListenableWorker
. It has exactly that interface. Rather than you having to call jobFinished()
in the end, we do the work for you when you resolve the ListenableFuture
.
https://developer.android.com/reference/androidx/work/ListenableWorker.html#startWork() is analogous to onStartJob
in your case.