Android Worker execute the job multiple times

2019-08-03 13:25发布

I use the following code to schedule a background job execute every 15 mins.

WorkManager workManager = WorkManager.getInstance();
PeriodicWorkRequest ulpBackup;

ulpBackup = new PeriodicWorkRequest
    .Builder(Ulp.class, 15, TimeUnit.MINUTES)
    .addTag(activity.getString(R.string.job_tag))
    .build();
workManager.enqueue(ulpBackup);

And here is the UlpBackup.class

public class UlpBackup extends Worker {
    private Integer responseCounter = 0;

    public UlpBackup() {}

    @NonNull
    @Override
    public Result doWork() {

        Log.d(logTag, "Starting periodic backup job";   
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        /** Read from local database and upload to firestore **/

        localdb.setAPIListener(new APIListener() {
            @Override
            public void OnSuccess() {
                responseCounter++;
                if (responseCounter == 5) {
                    countDownLatch.countDown();
                }
            }

            @Override
            public void OnFailure() {
                responseCounter++;
                if (responseCounter == 5) {
                    countDownLatch.countDown();
                }
            }
        });

        localdb.sync();

        try {
            countDownLatch.await(300, TimeUnit.SECONDS);
        } catch (Exception exception) {
            Log.e(logTag, "Error in user list backup job " + exception.getMessage());
            return Result.FAILURE;
        }

        Log.e(logTag, "Ulp backup completed");
        return Result.SUCCESS;
}

The above code works fine and the job happened roughly every 15 minutes as expected. The only thing that I do not understand is that every time the job execute multiple times, can someone explain why and how can i avoid it?

From the log:

09-15 23:33:37.514 8190-8410: Starting periodic backup job
09-15 23:33:37.520 8190-8414: Starting periodic backup job
09-15 23:33:37.561 8190-8412: Starting periodic backup job
09-15 23:33:37.568 8190-8413: Starting periodic backup job
...
...
09-15 23:33:38.183 8190-8414: Ulp backup completed
09-15 23:33:39.164 8190-8412: Ulp backup completed
09-15 23:33:39.580 8190-8413: Ulp backup completed
09-15 23:38:37.517 8190-8410: Ulp backup completed

2条回答
ら.Afraid
2楼-- · 2019-08-03 14:03

You can use this following method : enqueueUniquePeriodicWork(), and implement like that :

WorkManager.getInstance().enqueueUniquePeriodicWork("YOUR_TAG", ExistingPeriodicWorkPolicy.KEEP, workRequest);

Please notice that the second parameter is the key. The enum can be KEEP or REPLACE

查看更多
啃猪蹄的小仙女
3楼-- · 2019-08-03 14:03

I think i am late to answer this question and you might have tweaked it too.. But i am trying to help anyways..

The only thing that I do not understand is that every time the job execute multiple times, can someone explain why and how can i avoid it?

Explanation :

Everyone will be shocked if i will say :

  1. Googles android has no control over crores of processes running in real-time basis
  2. It even lacks to understand even say xyz process is completed? or running? or finished?
  3. This is it. And the outcome is the your issue

Oh my god.. It will lead to data-loss if i am doing any database operations then..!!??

Yes it results in data loss many times..

Does android and developers have any work around then...?

Yes, Prefer using IntentService.

What is so special in IntentService?

If a intentservice is already running and even if thousands of times it gets called... Then in that case too... all calls are kept in queue by android and processed one by one and one after another and after one is finished then only another waiting awakes and starts to execute it

Hope it helps.

查看更多
登录 后发表回答