Firebase JobDispatcher - how does it work compared to previous APIs (JobScheduler and GcmTaskService)?

15.8k views Asked by At

Background

Google has multiple solutions for job/task scheduling, such as JobScheduler and GcmTaskService. Each has its own advantages and disadvantages.

Recently, Google presented a new library called "Firebase JobDispatcher".

The problem

Sadly, there is very little to read about this new API. In fact, it's really hard to find anything about it.

Only thing I've found is their announcement video and a sample. But even their, there is not much to know about this API.

The questions

Looking at previous questions, investigations and comparisons I had with the other APIs (here, for example), I'd like to ask how the new API works and know what to take into consideration when using it:

  1. Can a job have parameters that stay with it and can even be modified when needed? They say in the sample "An optional Bundle of user-supplied extras. The default is an empty Bundle." Is this it? Can it be modified by the job upon execution of it?

  2. Can jobs be re-scheduled easily? It is said "A boolean indicating whether the Job should repeat" . How can it be chosen when to re-schedule? I've tried the sample, and chose "Recurring", but it doesn't seem to run again, only once.

  3. Can it be protected vs library's jobs (because of unique ids)?

  4. Does it needs extra care when updating the app (as previous APIs did)? Can jobs still be scheduled after an update of the app? Testing on the sample, it seems the jobs are completely gone after an update of the app. Can it be avoided?

  5. Does it need RECEIVE_BOOT_COMPLETED in case I want the job to still be scheduled even when the device is restarted? The sample seems to have it.

  6. Is it possible to get a list of all scheduled jobs and their information (including parameters), and be able to cancel specific/all of them and even modify them ?

  7. Will a job be removed upon clear-data operation of the app?

  8. Is it possible to tell the job that it's best that it will run in a range of time (example : between 7:00 and 8:00 in the morning)? It is mentioned "ExecutionWindowTrigger-which specifies a time window in which the Job should be executed". Is that it? What happens when it misses this window?

  9. The method onStartJob in JobService class return a boolean and the description for it is "whether there is more work remaining." What does it mean? What does the needsReschedule parameter of jobFinished method mean? Are they related to each other?

  10. Are there any restrictions I should know about? For example minimal & maximal values for each of the functions?

1

There are 1 answers

4
Flavio On

Actually Firebase Android JobDispatcher is a layer of abstraction around job scheduling engines on Android. And for now they only have one driver implementation for GCM Network Manager. That means currently it behaves the same way as GCM Network Manager behaves. Hopefully in the future more Drivers will be implemented.

1. Can a job have parameters that stay with it and can even be modified when needed? They say in the sample "An optional Bundle of user-supplied extras. The default is an empty Bundle." . Is this it? Can it be modified by the job upon execution of it?

  1. Yes, Job.Builder has method setExtras with arbitrary bundle which later may be accessed via jobParameters.getExtras(). You cannot modify the bundle (jobParameters contains only getters). You could reschedule your job with flag setReplaceCurrent(true) and specify a new bundle.

2. Can jobs be re-scheduled easily ? It is said "A boolean indicating whether the Job should repeat" . How can it be chosen when to re-schedule? I've tried the sample, and chose "Recurring", but it doesn't seem to run again, only once.

  1. To re-schedule job you need specify setRecurring(true), setTrigger(Trigger.executionWindow(10, 20))

This becomes triggered as soon as the window start deadline is reached, and drivers are encouraged to run the Job before the window end if possible.

3. Can it be protected vs library's jobs (because of unique ids) ?

  1. Job tags must be unique in your application. Other apps on the phone have their own 'endpoints' (package name/service name). To see all scheduled/finished jobs for GooglePlayDriver please use

    adb shell dumpsys activity service GcmService

4. Does it needs extra care when updating the app (as previous APIs did) ? Can jobs still be scheduled after an update of the app ? Testing on the sample, it seems the jobs are completely gone after an update of the app. Can it be avoided?

  1. As for GCM Network Manager GooglePlayDriver doesn't reschedule Jobs after Google Play Services or the app is updated. Here is an open issue for this. So for now this is your responsibility.

5. Does it need RECEIVE_BOOT_COMPLETED in case I want the job to still be scheduled even when the device is restarted? The sample seems to have it.

  1. Yes you need such permission. Builder has a parameter to control the behavior: setLifetime(Lifetime.FOREVER | UNTIL_NEXT_BOOT) Of course if you're going to create your own driver you'll have to take care of the lifetime yourself.

6. Is it possible to get a list of all scheduled jobs and their information(including parameters), and be able to cancel specific/all of them and even modify them ?

  1. No, the same as for GCM Network Manager. But you could track all jobs yourself somehow while scheduling them to play services.

7. Will a job be removed upon clear-data operation of the app?

  1. Yes, the job will be removed. Probably in the previous versions of google play services it behaved differently.

8. Is it possible to tell the job that it's best that it will run in a range of time (example : between 7:00 and 8:00 in the morning) ? It is mentioned "ExecutionWindowTrigger-which specifies a time window in which the Job should be executed" . Is that it? What happens when it misses this window?

  1. Well, you could setup an alarm to be fired at 7:00 and schedule a non-recurring job with executionWindow(0, 60*60). The job will run between 7:00 - 8:00.
    You cannot use recurring job because

    windowStart - The earliest time (in seconds) the job should be considered eligible to run. Calculated from when the job was scheduled (for new jobs) or last run (for recurring jobs).

Also, ExecutionWindowTrigger specifies approximate time. It's not guaranteed it would run at the given window. If it misses the window the job will run any time later.

9. The method "onStartJob" in "JobService" class return a boolean and the description for it is "whether there is more work remaining." . What does it mean? What does the "needsReschedule" parameter of "jobFinished" method mean? Are they related to each other?

  1. if onStartJob returns false that means you completed your work. No Need to invoke jobFinished. The RESULT_SUCCESS is sent automatically.

if onStartJob returns true that means you started a thread and waiting for results. As soos as you're done you must invoke jobFinished to inform google play services whether the job should be rescheduled or not. If yes the job will be rescheduled depending on RetryStrategy.

10. Are there any restrictions I should know about? For example minimal&maximal values for each of the functions?

    • onStartJob should offload work to another thread of execution as soon as possible. They provide a SimpleJobService as an example of what is expected from you.
    • There is no Driver implementation for Lollipop's JobScheduler. Also need to handle the situation when google play services are not available, we should probably implement Driver based on AlarmManager.