'CoroutineWorker crashes with no getForegroundInfo
I'm attempting to migrate some network calls to WorkManager CoroutineWorkers, to take advantage of the automatic retries on failure and scheduling powers.
This is the worker I wrote:
@HiltWorker
class RefreshSubscriptionDetailsWorker
@AssistedInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters
) :
CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
return Result.success()
}
}
The code in BaseApplication which configures the request:
val refreshSubscriptionDetailsRequest =
OneTimeWorkRequestBuilder<RefreshSubscriptionDetailsWorker>()
.build()
WorkManager
.getInstance(applicationContext)
.enqueue(refreshSubscriptionDetailsRequest)
I've followed the instructions in the documentation to ensure Hilt is injected correctly:
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
@Inject lateinit var workerFactory: HiltWorkerFactory
override fun getWorkManagerConfiguration() =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
The problem I'm having is that when it's queued, it crashes with the below exception:
Fatal Exception: java.lang.IllegalStateException: Not implemented
at androidx.work.CoroutineWorker.getForegroundInfo$suspendImpl(CoroutineWorker.kt:100)
at androidx.work.CoroutineWorker.getForegroundInfo()
at androidx.work.CoroutineWorker$getForegroundInfoAsync$1.invokeSuspend(CoroutineWorker.kt:134)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
I would expect that I would need to implement the getForegroundInfo function if I was going to be using an Expedited request, but I shouldn't need to in this instance, as I'm not asking for an expedited request, or so I've been led to believe by the absense of getForegroundInfo implementations in tutorials, and the patch notes which indicate that you need a ForegroundService for Expedited requests: Patch notes
I'm using WorkManager 2.7.1, Hilt 2.41, trying to run it on Android versions 8 through 12.
Solution 1:[1]
remove .setExpedited(OutOfQuotaPolicy.DROP_WORK_REQUEST) from your workrequest. val workRequest = OneTimeWorkRequestBuilder<...>() .addTag(...) .build() just do that , it was work for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krupal Bhuva |
