Поэтому я использую модификацию с сопрограммами и объявил метод в сервисе:
@GET("notifications")
suspend fun getNotificationsAsync(@QueryMap paramsHashMap:HashMap<String,String>): Response<JsonObject>
В репо:
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)
Что нужно учесть здесь:
- В DataSource я создал отдельную область, также пробованную с
GlobalScope
и viewModelScope
, но получаю то же исключение. - Но если я вызываю этот API из
ViewModel
напрямую вместо DataSource
, он работает нормально. - Текущая версия
kotlin
Я использую это: 1.3.61
Дайте мне знать, если требуется больше кода.