Enqueue unique work

This commit is contained in:
M66B 2019-03-03 19:25:53 +00:00
parent 1e7a11da91
commit a138088469
2 changed files with 4 additions and 24 deletions

View File

@ -12,8 +12,8 @@ import javax.mail.Store;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.work.Data; import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest; import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
@ -87,23 +87,13 @@ public class WorkerFolderSync extends Worker {
String tag = WorkerFolderSync.class.getSimpleName() + ":" + aid; String tag = WorkerFolderSync.class.getSimpleName() + ":" + aid;
Log.i("Queuing " + tag); Log.i("Queuing " + tag);
try {
for (WorkInfo info : WorkManager.getInstance().getWorkInfosByTag(tag).get())
if (!info.getState().isFinished()) {
Log.i("Already queued " + tag);
return;
}
} catch (Throwable ex) {
Log.w(ex);
}
Data data = new Data.Builder().putLong("account", aid).build(); Data data = new Data.Builder().putLong("account", aid).build();
OneTimeWorkRequest workRequest = OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(WorkerFolderSync.class) new OneTimeWorkRequest.Builder(WorkerFolderSync.class)
.addTag(tag) .addTag(tag)
.setInputData(data) .setInputData(data)
.build(); .build();
WorkManager.getInstance().enqueue(workRequest); WorkManager.getInstance().enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, workRequest);
Log.i("Queued " + tag); Log.i("Queued " + tag);
} }

View File

@ -15,8 +15,8 @@ import javax.mail.event.StoreListener;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.work.Data; import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest; import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import androidx.work.Worker; import androidx.work.Worker;
import androidx.work.WorkerParameters; import androidx.work.WorkerParameters;
@ -121,23 +121,13 @@ public class WorkerOperations extends Worker {
String tag = WorkerOperations.class.getSimpleName() + ":" + fid; String tag = WorkerOperations.class.getSimpleName() + ":" + fid;
Log.i("Queuing " + tag); Log.i("Queuing " + tag);
try {
for (WorkInfo info : WorkManager.getInstance().getWorkInfosByTag(tag).get())
if (!info.getState().isFinished()) {
Log.i("Already queued " + tag);
return;
}
} catch (Throwable ex) {
Log.w(ex);
}
Data data = new Data.Builder().putLong("folder", fid).build(); Data data = new Data.Builder().putLong("folder", fid).build();
OneTimeWorkRequest workRequest = OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(WorkerOperations.class) new OneTimeWorkRequest.Builder(WorkerOperations.class)
.addTag(tag) .addTag(tag)
.setInputData(data) .setInputData(data)
.build(); .build();
WorkManager.getInstance().enqueue(workRequest); WorkManager.getInstance().enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, workRequest);
Log.i("Queued " + tag); Log.i("Queued " + tag);
} }