yigit / android-priority-jobqueue

A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
3.4k stars 395 forks source link

creating a unique job #433

Closed kpradeepkumarreddy closed 6 years ago

kpradeepkumarreddy commented 6 years ago

what happens if a job with same Id already exists ? I want to have only job with the same Id. when i try to add a new job with same Id then the old job should be replaced with new one .

How can i create a job with unique Id ? Is there anything like setUnique(true) for a job ?

kalpeshp0310 commented 6 years ago

You can create a Single Instance Job using singleInstanceBy(String instanceId). have a look at Single Instance Jobs section.

Note: You need second job to replace older job. but library will cancel second job if there is a job in queue with same instanceId, instead of replacing it.

kpradeepkumarreddy commented 6 years ago

I'm using the following piece of code to ensure that a job is not running with same id. Is this the correct way to do it ?

JobStatus jobStatus = Jobmanager.getJobStatus("ID");
 if (jobStatus.ordinal() == JobStatus.UNKNOWN.ordinal()) {
          Jobmanager.addJob(new Job());
  }
kalpeshp0310 commented 6 years ago

Yeah, this approach also works. Only thing to note is that you have to run this code on non main thread, other wise it may cause UI jank.

kpradeepkumarreddy commented 6 years ago

Thanks.