1

I'd like to know if there's a recommended way of retrieving a list of active jobs in https://github.com/yigit/android-priority-jobqueue

That way, if I have persistent jobs still waiting, I can let the user know which ones.

gbhall
  • 13,139
  • 9
  • 37
  • 42

1 Answers1

6

We don't provide such an API because it may lead to mis-uses. e.g. checking a job if it exists to decide whether to add or not (because that is recipe for race conditions).

If you really need this, you can override the persistent queue and provide your own queue where you can track job additions/removals. See the JobManager configuration WIKI for details:

https://github.com/yigit/android-priority-jobqueue/wiki/Job-Manager-Configuration

edit: An example race condition

Someone may write a code like this:

if (!jobManager.hasJobOfType(ImportantJob.class)) {
    jobManager.addJob(new ImportantJob());
} else {
    // it is already added, no need to re add
}

There is a potential race condition here where hasJobOfType returns true but meanwhile Job is about to be canceled. Now, there is no job to run :/. Another race condition is, somewhere else the code might have called addJobInBackground so it will be added but it is not yet added at the time of the hasJobOfType call.

The best solution for this kind of cases is to re-create another job in onCancel or manually handling the cancelation of previous jobs in onRun method (with a global counter etc). Another option is to use cancelAsync method with its callback version.

http://yigit.github.io/android-priority-jobqueue/javadoc/index.html

orange01
  • 1,584
  • 1
  • 16
  • 28
yigit
  • 37,683
  • 13
  • 72
  • 58
  • Thank you Yigit. I understand, don't worry I have a few alternative solutions that will still work. – gbhall Dec 21 '15 at 01:52
  • PS I've just woken up, but curiosity wise, could you provide an example of a typical race condition scenario if you were to allow checking a job? – gbhall Dec 21 '15 at 01:54