作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我使用协程改造,并在服务中声明了一个方法:
@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)
这里要考虑的事情:
GlobalScope
和 viewModelScope
但得到相同的异常。ViewModel
而不是 DataSource
调用这个 API,它就可以正常工作。kotlin
版本是:1.3.61
如果需要更多代码,请告诉我。
最佳答案
地址为 here .我让它工作的唯一方法是从我暂停的函数中删除 inline
和 crossinline
。
关于android - kotlin.coroutines.intrinsics.CoroutineSingletons 不能转换为 retrofit2.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60956288/
所以我使用协程改造,并在服务中声明了一个方法: @GET("notifications") suspend fun getNotificationsAsync(@QueryMap params
我是一名优秀的程序员,十分优秀!