I send some event to backend in my app and this event shoudnt be messed if a problem like network lost or etc happens. so i use android work manager class and for any request i fire a OneTimeWorkRequest. i wish that when one request failes ,retry sendding that request (retry relative worker ) and following requests get wait on a queue until failed request get success and then all following requests get done by order. for this puprpose i first in my mainActivity class, create a WorkContinuation object. and then add comming workers to this WorkContinuation. i mean if WorkContinuation is not initilized, initilize it and if so, i add works to this WorkContinuation , problem is works not get queued and every work is doing its process independently, and does retry until gets done. this is the way i make worker samples and add them to WorkContinuation. any help with doing this in correct way will be appereciated.
creating work :
Constraints cons = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
Data.Builder data = new Data.Builder();
data.putLong("busStopId", stopId);
data.putBoolean("justStop", justStop);
data.putInt("state", state);
data.putString("time", Tools.getCurrentTimeWithOffset());
OneTimeWorkRequest sendWorker = new OneTimeWorkRequest.Builder(sendStateWorker.class)
.setConstraints(cons)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS)
.setInputData(data.build())
.build();
myActivity.addWork(sendWorker);
creatining WorkContinuation and adding work to it in main activity:
WorkContinuation workQueue;
method:
@SuppressLint("EnqueueWork")
public void addWork(OneTimeWorkRequest workRequest) {
if (workQueue == null) {
workQueue = WorkManager.getInstance(this)
.beginWith(workRequest);
workQueue.enqueue();
} else workQueue.then(workRequest).enqueue();
}