savepopulation
savepopulation

Reputation: 11921

Android-Priority-Jobqueue Surviving Orientation Change

I'm trying to implement the library below to survive orientation changes: https://github.com/yigit/android-priority-jobqueue

Here's my configuration:

 Configuration config = new Configuration.Builder(getApplication())
                .consumerKeepAlive(45)
                .maxConsumerCount(3)
                .minConsumerCount(1)
                .build();
        return new JobManager(config);

Here's my example job:

public class CounterJob extends Job {
    private int countTo;

    protected CounterJob(int countTo, Params params) {
        super(params);
        this.countTo = countTo;
    }

    @Override
    public void onAdded() {
        Log.e("counting to", "" + countTo);
    }

    @Override
    public void onRun() throws Throwable {
        Log.e("running job", "" + countTo);
        int total = 0;
        for (int i = 0; i < countTo; i++) {
            total += i;
        }

        Log.e("total", "" + total);
    }

    @Override
    protected void onCancel(int cancelReason, @Nullable Throwable throwable) {

    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
        return null;
    }
}

Here's an example Controller for counting:

public class CounterController {
    private JobManager mJobManager;

    public CounterController(JobManager jobManager) {
        this.mJobManager = jobManager;
    }

    public void count(int countTo) {
        mJobManager.addJobInBackground(new CounterJob(countTo, new Params(1).requireNetwork().persist()));
    }
}

And i call like:

@Override
protected void onResume() {
    super.onResume();
    mCounterController.count(1000000000);
}

When i rotate the device same job starts again. So if i'm not wrong, if i'm doing a network call, when orientation changed it'll duplicate the request.

I think there's a problem about my implementation. I tried to implement as example in library page. Any suggestions? Thanks.

Upvotes: 1

Views: 304

Answers (2)

I found the solution : you can give a group ID then a ID to the job so it won t be running again. Here is the javadoc :

/////// for the group ///////

/** * Sets the group id. Jobs in the same group are guaranteed to execute sequentially. * @param groupId which group this job belongs (can be null of course) * @return this */

public Params groupBy(String groupId) {
    this.groupId = groupId;
    return this;
}

////////for the ID ////////

/** * Sets the single instance id. If there is another Job with the same single id queued and * not yet running, this Job will get {@link Job#onCancel(int, Throwable)} called immediately after * {@link Job#onAdded()} and only the previous Job will run. That is, {@link Job#onRun()} * will only be called once. *

If no group id was set, one will be set automatically. * @param singleId which single instance group this job belongs to (can be null of course) * @return this */

public Params singleInstanceBy(String singleId) {
    this.singleId = singleId;
    return this;
}

You set it in the constructor methods

  public Step1Jobs() {
        super(new Params(Priority.LOW).requireNetwork().groupBy(STEPS).singleInstanceBy(STEP1));
    }

Upvotes: 0

yigit
yigit

Reputation: 38273

I'm guessing you are re-creating the job when you rotate so then you end up with 2 jobs. You should not enqueue it twice when activity changes configuration (a.k.a. rotation).

Upvotes: 2

Related Questions