0

So I have to download a bunch of image files from a server and I'm using Priority-Job-Queue. So far seems to works fine and I'm using a simple AsyncTask for the downloading part.

Since I want the images to be downloaded no matter what I only added RetryConstraint.RETRY on shouldReRunOnThrowable() callback. I have also added android.permission.RECEIVE_BOOT_COMPLETED permission on Manifest.xml

Is this the right/best way so if there's any kind of problem and some images aren't downloaded due to an error, job-queue will try to download them again and again until the job is finished with success ?

Thanks!

@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {

    return RetryConstraint.RETRY;
}
Mes
  • 1,671
  • 3
  • 20
  • 36
  • Note: this library is deprecated now, use WorkManager instead (for persistent jobs). For non-persistent jobs, use Corouties as recommended here https://github.com/yigit/android-priority-jobqueue – Birender Singh Aug 26 '19 at 06:15

1 Answers1

3

Looking at the source code there are cancelForDeadline and getRetryLimit() constraints, that you should satisfy in order to keep retrying your job.

For the first one you just don't overrideDeadlineToCancelInMs for Params object, so in that case cancelForDeadline is always false.

For the second one, you have to override getRetryLimit method in your job, like:

protected int getRetryLimit() {
    return Integer.MAX_VALUE;
}

And finally in case if you reached retry limit, you can schedule your job again:

@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
    if (cancelReason == CancelReason.REACHED_DEADLINE) {
        jobManager.addJobInBackground(new FetchImageJob());
    }
}

Now for me it looks like it will run until success.

romtsn
  • 11,704
  • 2
  • 31
  • 49