I used WorkManager version android.arch.work:work-runtime:1.0.0-beta03
.I used this workmanager for uploading files to server with dynamic interval time using OneTimeWorker.
This is what I do. Hope you can check it and tell me where i were wrong and alternative way to fix it.
First, I make a work request
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(MyWorkRequest.class)
.setInitialDelay(0), TimeUnit.SECONDS)
.addTag("UploadData")
.build();
WorkManager
.getInstance()
.getWorkInfoByIdLiveData(workRequest.getId())
.observeForever(workInfoObserverBackground);
WorkManager
.getInstance()
.enqueueUniqueWork("UploadData", ExistingWorkPolicy.REPLACE, workRequest);
Then, Define Observer to listen to its result. I also start next run here.
workInfoObserverBackground = workInfo -> {
if (workInfo != null){
switch (workInfo.getState()) {
case SUCCEEDED:
case FAILURE:
WorkManager.getInstance().getWorkInfoByIdLiveData(workRequest.getId()).removeObserver(workInfoObserverBackground);
WorkManager.getInstance().cancelUniqueWork("UploadData");
WorkManager.getInstance().cancelAllWorkByTag("UploadData");
workRequest = new OneTimeWorkRequest.Builder(MyWorkRequest.class)
.setInitialDelay(next_interval_time_received_from_api), TimeUnit.SECONDS)
.addTag("UploadData")
.build();
WorkManager
.getInstance()
.getWorkInfoByIdLiveData(workRequest.getId())
.observeForever(workInfoObserverBackground);
WorkManager
.getInstance()
.enqueueUniqueWork("UploadData", ExistingWorkPolicy.REPLACE, workRequest);
break;
}
}
};
After some minutes (i cannot record it, because it randomly happen) it crashed and this is stacktrace collected from logcat.
1-15 21:46:14.806 25964-26054/com.example.app E/AndroidRuntime: FATAL EXCEPTION: pool-8-thread-1
Process: com.example.app, PID: 25964
android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed.
at android.database.CursorWindow.<init>(CursorWindow.java:108)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:139)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
at androidx.work.impl.model.WorkSpecDao_Impl.getInputsFromPrerequisites(WorkSpecDao_Impl.java:1112)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:171)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:124)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
My interval time is always 1 sec. Maybe old task hang with next task and cannot be destroy?