I'm trying to get result from WorkManager in suspend function by this way
suspend fun uploadLogs(filePath: String): String {
val request = createRequest(createInputLogsData(filePath))
workManager.enqueue(request).await()
val url = workManager.getWorkInfoById(request.id).await().outputData.getString(KEY_URL)
return url
}
But looks like await() function didn't work. After call await() state of request is still ENQUEUED.
I need get result synchronously from WorkManager in this coroutine context.
Maybe I doing something wrong?
What you're trying to achieve looks like an immediate task while WorkManager
is designed for deferred tasks, see background processing guide for more details about the difference.
WorkManager doesn't guarantee that the request will be executed immediately. Actually, the request can be executed when your app is terminated or even after the device is rebooted. There's no way to await the request completion. WorkManager.enqueue()
method doesn't allow you to await the request result, it awaits only the enqueue operation completion.
So, if it's an immediate task you can use a coroutine to execute it, it's the recommended way. If it's a deferred task, use WorkManager
and move the result processing logic into the worker.