Coroutine Worker Not Updating Progress Sometimes with setProgress in WorkManager
I am facing an issue with WorkManager where the progress updates are not being sent consistently from a coroutine worker, even though I am calling setProgress correctly.
It sometimes not sending progress why?. For instance fileuplaodsatte is in progress of file, then completed state. The completed state is not sending sometimes why? Also sometime when file progress is 80 percentage after i update 100% then instantly the file is completed state, the 100% state is skipped the completed state is updated why?.
private suspend fun updateUploadState(messageId: Long, state: FileUploadState) {
try {
if (!isStopped) {
withContext(Dispatchers.IO) {
setProgress(
workDataOf(
"messageId" to messageId,
"state" to serializeFileUploadState(state)
)
)
}
}
} catch (_: IllegalStateException) { }
}
I read progress using
val workInfoList by WorkManager.getInstance(context)
.getWorkInfosFlow(WorkQuery.fromUniqueWorkNames("visual_media_upload_${viewModel.chatId}_${message.id}"))
.collectAsState(null)
LaunchedEffect(workInfoList) {
// Find the relevant work and retrieve the messageId and state
workInfoList?.firstOrNull()?.let { workInfo ->
val workerState = workInfo.state // WorkInfo state is important here
if (workerState == WorkInfo.State.CANCELLED) {
CoroutineScope(Dispatchers.IO).launch {
viewModel.repository.updateMessage(
message.id,
ChatMessageStatus.QUEUED_MEDIA_RETRY
)
}
} else {
val progressMessageId = workInfo.progress.getLong("messageId", -1)
val state = workInfo.progress.getString("state")
Log.e(TAG,"UI state: $state")
if (progressMessageId != -1L && state != null) {
viewModel.updateUploadState(
progressMessageId,
deserializeFileUploadState(state)
)
}
}
}
}
Logs: 100% (Uploaded size 127784) not updated even i am sending 100%
2025-01-30 16:34:54.553 22955-22955 UI state: null
2025-01-30 16:34:54.557 22955-22955 UI state: null
2025-01-30 16:34:54.764 22955-22955 UI state: {"type":"com.super6.pot.ui.chat.viewmodels.FileUploadState.Started"}
2025-01-30 16:34:57.022 22955-22955 UI state: {"type":"com.super6.pot.ui.chat.viewmodels.FileUploadState.InProgress","uploadedSize":127784}
Progress: 76%, File Size: 166203, Uploaded: 127784
2025-01-30 16:34:57.519 22955-22955 UI state: {"type":"com.super6.pot.ui.chat.viewmodels.FileUploadState.Completed"}
Source: View source