Scheduled job executes multiple time in Evernote- AndroidJob
I found the answer to the whole thing. I am answering this, incase anyone else faces the same issue.
Ok, so according to my logic above, this is what was happening :
1) User opens the app for very first time, a job is scheduled in the OS, which runs till infinity.
2) User closes and opens the app again, another job is scheduled in the OS.
3) By the 101th time user opens the app, there are now 100 jobs scheduled and the app goes to schedule the 101th job, which throws an exception, because android only allows an app to schedule 100 jobs(in newer OS versions).
So, what did I do to solve this ?
I modified my schedule()
to look like the following :
public static void schedule() {
final long INTERVAL = 900000L;
final long FLEX = 300000L;
jobId = new JobRequest
.Builder(LocationUpdateJob.TAG)
.setPeriodic(INTERVAL, 300000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setPersisted(true)
.setUpdateCurrent(true)
.build()
.schedule();
}
What is different here, from the old way I scheduled is : .setUpdateCurrent(true)
So what that does is, every time a job is scheduled with a tag, it replaces any existing jobs with the same tag, with the new job, therefore only one job is scheduled to execute with that tag, ie,the job's Tag is made unique.
There is a very brief and good explanation here, please read it.
In case of WorkManager, I have added .beginUniqueWork() before .enqueue() and it worked for me.
Constraints myConstraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
Data.Builder basicWorkerInputDataBuilder = new Data.Builder();
OneTimeWorkRequest otwRequest = new OneTimeWorkRequest.Builder(BasicOneTimeWorker.class)
.setConstraints(myConstraints).addTag("BasicOneTimeWorker")
.setInputData(basicWorkerInputDataBuilder.build());
WorkManager.getInstance().beginUniqueWork("uniqueBasicOneTimeWork",
ExistingWorkPolicy.REPLACE,
otwRequest).enqueue();
The below discussion helped me to find the solution.
https://github.com/googlecodelabs/android-workmanager/issues/16
https://issuetracker.google.com/issues/111569265