JobSchedulerCompat
JobSchedulerCompat copied to clipboard
Bug fix: Rescheduling has problem (refers to issues #25 and #18)
The problem is occurred when you've recently scheduled an unmetered network triggered periodic job and after a while you want to schedule another job(e.g. an unmetered network triggered periodic job as the second job).
Issue : When the 2'nd job is going to add, the job scheduler stops the first job and in the following needs to reschedule it again. The scheduler probably has missed some operations within rescheduling process.
Solution : If you review the "scheduleJob(JobStatus job) { ... }" method in the "me.tatarka.support.internal.job.JobServiceCompat" class, you can find out there is a bug in the "me.tatarka.support.internal.job.JobSchedulerService" class exactly the "rescheduleJob(JobStatus job, boolean wasFailure) { ... }" method and you must change it. In the following there is a new implementation of the mentioned method.
In the JobSchedulerService class ==>
private void rescheduleJob(JobStatus job, boolean wasFailure) {
JobStatus newJob;
if (wasFailure) {
newJob = rescheduleFailedJob(job);
} else {
newJob = reschedulePeriodicJob(job);
}
JobStore jobStore = JobStore.initAndGet(this);
synchronized (jobStore) {
jobStore.remove(job);
jobStore.add(newJob);
}
TimeReceiver.setAlarmsForJob(this, newJob);
if (job.hasIdleConstraint()) {
IdleReceiver.setIdleForJob(this, newJob);
}
/**
* The following constraints must be set and it is clear that those probably were forgotten
*/
if (newJob.hasConnectivityConstraint() || newJob.hasUnmeteredConstraint()) {
NetworkReceiver.setNetworkForJob(this, newJob);
ReceiverUtils.enable(this, NetworkReceiver.class);
}
if (job.hasChargingConstraint()) {
PowerReceiver.setPowerForJob(this, job);
ReceiverUtils.enable(this, PowerReceiver.class);
}
if (job.isPersisted()) {
ReceiverUtils.enable(this, BootReceiver.class);
}
}
It is tested for 10 unmetered network triggered periodic job ( every job had a period of 500ms) and their worked without any error nonstop at least for 30 minutes.
I would suggest to open a pull request that fixes the bug.
hope you update this project to solve this bug
I'll merge a pull request if I get one, though I advise anyone using this to look at https://developers.google.com/cloud-messaging/network-manager instead.
yep, the Network Manager introducet at the I/O looks very promising! :)
i'v tested your debug code, this error still occurred, i think it is not because the reason you'v mentioned. when i test evant' s example app , scheduled about three periodical jobs , when you press the "TASKFINISHED" button again and again to emulate that the job is finished, then sometime this error occurred. i think it is something wrong or not robust in the JobSchedulerService , such as the JobServiceConnection , bindService and unbindService.