Enqueue unique work

pull/152/head
M66B 6 years ago
parent 1e7a11da91
commit a138088469

@ -12,8 +12,8 @@ import javax.mail.Store;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
@ -87,23 +87,13 @@ public class WorkerFolderSync extends Worker {
String tag = WorkerFolderSync.class.getSimpleName() + ":" + aid;
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();
OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(WorkerFolderSync.class)
.addTag(tag)
.setInputData(data)
.build();
WorkManager.getInstance().enqueue(workRequest);
WorkManager.getInstance().enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, workRequest);
Log.i("Queued " + tag);
}

@ -15,8 +15,8 @@ import javax.mail.event.StoreListener;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
@ -121,23 +121,13 @@ public class WorkerOperations extends Worker {
String tag = WorkerOperations.class.getSimpleName() + ":" + fid;
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();
OneTimeWorkRequest workRequest =
new OneTimeWorkRequest.Builder(WorkerOperations.class)
.addTag(tag)
.setInputData(data)
.build();
WorkManager.getInstance().enqueue(workRequest);
WorkManager.getInstance().enqueueUniqueWork(tag, ExistingWorkPolicy.KEEP, workRequest);
Log.i("Queued " + tag);
}

Loading…
Cancel
Save