gpt4 book ai didi

android - kotlin.coroutines.intrinsics.CoroutineSingletons 不能转换为 retrofit2.Response

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

所以我使用协程改造,并在服务中声明了一个方法:

@GET("notifications")
suspend fun getNotificationsAsync(@QueryMap paramsHashMap:HashMap<String,String>): Response<JsonObject>

在 repo 中:

suspend inline fun <reified T> notificationApiForPagingAsync(hashMapParams: HashMap<String,String>, onPrepare:()->Unit, onSuccess:(list:List<T>)->Unit, onError:(errorMessage:String)->Unit){
// crashing on below line
val response=notificationService.getNotificationsAsync(hashMapParams)

safeApiCallForPaging(response,onPrepare = onPrepare
, onSuccess = {
val listOfNotification=it.data?.fromJsonTo<List<Notification>>("notifications")?: emptyList()
onSuccess(listOfNotification as @kotlin.ParameterName(name = "list") List<T>)
}, onError = onError)
}

然后像这样从 DataSource 调用:

coroutineScope.launch(Dispatchers.IO) {
makeLoadAfter(params, callback) // notificationApiForPagingAsync of repo will be called from here
}

它给出了以下错误:

AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.package.name.dev, PID: 20173
java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to retrofit2.Response
at com.package.name.notification.NewNotificationViewModel$configurePaging$1.invoke(NewNotificationViewModel.kt:53)
at com.package.name.notification.NewNotificationViewModel$configurePaging$1.invoke(NewNotificationViewModel.kt:15)
at com.package.name.common.paging.GenericDataSource.makeLoadAfter(GenericDataSource.kt:85)
at com.package.name.common.paging.GenericDataSource$loadAfter$1.invokeSuspend(GenericDataSource.kt:32)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-2
Process: com.package.name.dev, PID: 20173
kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[LimitingDispatcher@53edd6d[dispatcher = DefaultDispatcher], Continuation at com.package.name.common.paging.GenericDataSource$loadAfter$1.invokeSuspend(GenericDataSource.kt:32)@a30868f]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
at kotlinx.coroutines.DispatchedTask.handleFatalException$kotlinx_coroutines_core(Dispatched.kt:278)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:249)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
Caused by: java.lang.IllegalStateException: Job StandaloneCoroutine{Cancelling}@d4d1184 is already complete or completing, but is being completed with CompletedExceptionally[kotlinx.coroutines.JobCancellationException: StandaloneCoroutine is cancelling; job=StandaloneCoroutine{Cancelling}@d4d1184]
at kotlinx.coroutines.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.kt:788)
at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:111)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:334)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594) 
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740) 
Caused by: kotlinx.coroutines.JobCancellationException: StandaloneCoroutine is cancelling; job=StandaloneCoroutine{Cancelling}@d4d1184
Caused by: java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to retrofit2.Response
at com.package.name.notification.NewNotificationViewModel$configurePaging$1.invoke(NewNotificationViewModel.kt:53)
at com.package.name.notification.NewNotificationViewModel$configurePaging$1.invoke(NewNotificationViewModel.kt:15)
at com.package.name.common.paging.GenericDataSource.makeLoadAfter(GenericDataSource.kt:85)
at com.package.name.common.paging.GenericDataSource$loadAfter$1.invokeSuspend(GenericDataSource.kt:32)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

这里要考虑的事情:

  1. 在 DataSource 中,我创建了一个单独的范围,也尝试使用 GlobalScopeviewModelScope 但得到相同的异常。
  2. 但如果我直接从 ViewModel 而不是 DataSource 调用这个 API,它就可以正常工作。
  3. 我目前使用的kotlin版本是:1.3.61

如果需要更多代码,请告诉我。

最佳答案

地址为 here .我让它工作的唯一方法是从我暂停的函数中删除 inlinecrossinline

关于android - kotlin.coroutines.intrinsics.CoroutineSingletons 不能转换为 retrofit2.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60956288/

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