gpt4 book ai didi

抛出异常时Android CoroutineWorker重试

转载 作者:行者123 更新时间:2023-12-02 12:41:00 25 4
gpt4 key购买 nike

我正在使用以下代码发出数据库和网络请求。问题是即使我正在处理异常,异常仍会传播到 Worker 并且 Worker 返回 FAILURE。

class TaskSyncWorker @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted private val params: WorkerParameters,
private val dataManager: DataManager
) : CoroutineWorker(appContext, params) {

companion object {
const val TASK_LONG_ID = "taskLongId"
}

override suspend fun doWork(): Result {
var success = false
val taskLongId = inputData.getLong(TASK_LONG_ID, -1L)
if (taskLongId != -1L) {
coroutineScope {

val getTaskFromDbJob = async {
dataManager.getTask(taskLongId)
}

val task = getTaskFromDbJob.await()

val taskNetworkSyncJob = async {
dataManager.syncSingleTask(
TaskSync(
dataManager.getUserId(),
dataManager.getAccessToken(),
task
)
)
}

try {
val response = taskNetworkSyncJob.await()
if (response.isSuccessful) {
//val jsonObject = response.body()!!.asJsonObject
success = true
} else {
success = false
}
} catch (e: Exception) {
if (e is CancellationException) {
Timber.d("Job was Cancelled....")
}
//Log exception
Timber.e("Handling Exception......")
Timber.e(e)
}
}
} else {
success = true
}

return if (success)
Result.success()
else
Result.retry()
}

@AssistedInject.Factory
interface Factory : ChildWorkerFactory
}

我想在出现异常时重试 worker,但它失败了。

E/TaskSyncWorker$doWork: Handling Exception......
E/TaskSyncWorker$doWork: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1409)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:593)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:700)
at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:698)
at com.google.gson.internal.bind.TypeAdapters$35$1.read(TypeAdapters.java:894)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
E/WM-WorkerWrapper: Work [ id=31e56edb-bf4f-461a-b1fc-339c80266b6e, tags={ com.vicky7230.tasker.worker.TaskSyncWorker } ] failed because it threw an exception/error
java.util.concurrent.ExecutionException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:298)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 1 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1409)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:593)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:700)
at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:698)
at com.google.gson.internal.bind.TypeAdapters$35$1.read(TypeAdapters.java:894)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:919) 
I/WM-WorkerWrapper: Worker result FAILURE for Work [ id=31e56edb-bf4f-461a-b1fc-339c80266b6e, tags={ com.vicky7230.tasker.worker.TaskSyncWorker } ]

最佳答案

您需要使用supervisorScope 来避免向上传播异常。将 coroutineScope{...} 更改为 supervisorScope{...}

关于抛出异常时Android CoroutineWorker重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com