Friday, 29 January 2021

How to observer Android Worker progress for chained workers

My current Android application employs Workers for its background processes.

api "androidx.work:work-runtime:2.4.0"
api "androidx.work:work-runtime-ktx:2.4.0"

I chain multiple workers together and would like to report on each workers progress

I have an issue in that I only get a single progress update from each worker

when each worker publish progress twice as follows:-

override suspend fun doWork(): Result = coroutineScope { val startTime: Long = System.nanoTime()

    val progressName = "$PROGRESS_PRE_PIX$workerName"

    withContext(Dispatchers.IO) {
        try {
            setProgressAsync(workDataOf(progressName to 0))
            doActualWork()
        } finally {
            setProgressAsync(workDataOf(progressName to 100))
        }
    }
}

I observeForever the workInfos from my chained workers continuation as shown here :-

 workContinuation.workInfosLiveData.observeForever { workInfos ->
                workInfos.forEach { workInfo ->
                    Timber.e("xoox - $workInfo")
                }
            }

I have verbose worker logging configured and each worker generates these log entries

2021-01-26 13:30:15.956  D/WM-WorkProgressUpdater: Updating progress for c9ef0d9a-f253-4403-944d-b9afb1a043ef (Data {PROGRESS-MyWorker : 0, })
2021-01-26 13:30:16.151  D/WM-WorkProgressUpdater: Updating progress for c9ef0d9a-f253-4403-944d-b9afb1a043ef (Data {PROGRESS-MyWorker : 100, })

however my observer only logs out 1 entry

xoox - WorkInfo{mId='70251d4e-882d-4ab8-946f-20cfba2d767a', mState=RUNNING, mOutputData=Data {}, mTags=[com.mendeley.refmanager.background.work.worker.GroupWorker], mProgress=Data {PROGRESS-MyWorker : 0, }}

What am I doing wrong?

Why dont I see the setProgressAsync(workDataOf(progressName to 100)) logged?



from How to observer Android Worker progress for chained workers

No comments:

Post a Comment