修改协程用例的返回类型

问题描述

最初,我是使用rxjava编写的api调用,现在我要通过暂停函数将其转换为使用kotlin协程。

我正在跟踪用例模式,其中用例负责线程处理。

我希望我的基本用例{{​​1}}非常通用,以返回实际api调用的类型。

如何修改我的UseCaseSuspend以返回任何类型,我想目前我已将其设置为返回UseCaseSuspend,这是我要更改的内容。

OrderRepository-我的api调用,它返回Unit

GenericResponse

用于扩展UseCaseSuspend的OnStUseCaseSuspend

这是 suspend fun notifyOnStSuspend(serviceType: String,id: String,action: TimestampedAction): GenericResponse = serviceSuspend.serviceOrderOnSt(serviceType,id,action) 上的错误- run

Return type is 'GenericResponse',which is not a subtype of overridden public abstract suspend fun run(params: OnStUseCaseSuspend.Params): Unit defined in com.i6systems.in2plane.clean.domain.UseCaseSuspend

UseCaseSuspend-我应该更改此属性,以根据API调用返回任何响应,而不是特定于Ge​​nericResponse

class OnStUseCaseSuspend @Inject constructor(
    private val orderRepository: OrderRepository
) : UseCaseSuspend<GenericResponse,OnStUseCaseSuspend.Params> (){
    override suspend fun run(params: Params) =
        orderRepository.notifyOnStSuspend(params.serviceType,params.id,params.action)

    data class Params(val serviceType: String,val id: String,val action: TimestampedAction)
}

我希望保持abstract class UseCaseSuspend <out Type,in Params> where Type : Any { abstract suspend fun run(params: Params) operator fun invoke(params: Params,onResult: (m: Unit) -> Unit = {}) { val job = GlobalScope.async(Dispatchers.IO) { run(params) } GlobalScope.launch(Dispatchers.Main) { onResult(job.await()) } } class None } 的通用性,以便响应可以是任何内容。

如何进行波纹管链接,但我没有使用任何一种

我指的是https://github.com/android10/Android-CleanArchitecture-Kotlin/blob/master/app/src/main/kotlin/com/fernandocejas/sample/core/interactor/UseCase.kt

谢谢 R

编辑:

我在UseCaseSuspend中为run函数添加了返回类型,这给我job.await()错误

enter image description here

这是演示者,使用Onstusecase,响应类型为UseCaseSuspend,但应为Unit

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)