I'm using CoroutineWorker
for background task. Here is the code snippet
class SimpleWorker(context: Context) : CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result = coroutineScope{
//obtain settings info saved as local file
val settingsInfo = obtainSettings(context)
if(null == settingsInfo) {
Result.failure()
}
Log.i(TAG, "Valid settings found, proceed")
val isUploadEnabled = settingsInfo.isUploadEnabled //error when settingInfo is null
}
}
In the event of null settings, I would expect the worker to send failure signals and stop running rest of the code. Instead it proceeds and breaks further down. Is Result.failure()
not doing what it is meant to be doing or do I miss something?
You have to actually call return
with the Result.failure()
object:
if(null == settingsInfo) {
return@coroutineScope Result.failure()
}
Otherwise you're just creating a Result.failure()
object and not doing anything with it.