Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65902651/how-to-observer-android-worker-progress-for-chained-workers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...