如何处理rxjava和协同程序

1mrurvl1  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(237)

我在我的项目中使用rxjava,现在我正在将逻辑从可观察更改为协同路由。但我被困在一个点上,用户界面点击是基于rx的,api是基于挂起的,它不喜欢它。
有什么建议可以帮我解决吗
fragment.java-这是我的ui按钮

@Override
    public Observable<Void> getStartSeeingObservable() {
        return RxView.clicks(dataViewHolder.btnStartSeeing);
    }

演示者.kt

subscriptions += view.startSeeingObservable
        .onBackpressureLatest()
        .doOnNext { view.showLoader(false) }
        .flatMap {
            if (!hasOpenInopIncidents()) {
                startSeeingUseCase(StartSeeingUseCase.Params(equipmentProvider.get()!!)) // this use case is not based on coroutines and its response is not an observable
            } else {
                val incidentOpenResponse = GenericResponse(false)
                incidentOpenResponse.error = OPEN_INCIDENTS
                Observable.just(incidentOpenResponse)
            }
        }
        .subscribe(
            { handleStartSeeingClicked(view, it) },
            { onStartSeeingError(view) }
        )

使用案例

@PerApp
class StartSeeingUseCase  @Inject constructor(
private val currentOrderStorage: CurrentOrderStorage,
private val seeOrderRepository: SeeOrderRepository,
private val app: App
): UseCaseCoroutine<GenericResponse, StartSeeingUseCase.Params>() {

    override suspend fun run(params: Params): GenericResponse {

        val startTime = DateTime()
        val action = TimestampedAction(
            app.session.user.id, null, startTime
        )
        try {
            return seeOrderRepository.startSeeing(
                currentOrderStorage.seeOrder!!.id,
                action
            )
        } catch (exception: Exception) {
            exception.message?.let { onError(it) }
            return GenericResponse(false)
        }
    }

    private fun onSuccess(startTime: DateTime, equipment: Equipment) {
        if (currentOrderStorage.getSeeOrder() == null) return
        currentOrderStorage.getSeeOrder()!!.setStatus(SeeOrderData.STATUS_SeeING)
        equipment.times.start = startTime
        app.saveState()
    }

    private fun onError(errorMessage: String) {
        Timber.e(errorMessage, "Error calling started seeing! %s", errorMessage)
    }

    data class Params(val equipment: Equipment)
}

abstract class UseCaseCoroutine<out Type, in Params> where Type : Any {

    abstract suspend fun run(params: Params): Type

    operator fun invoke(params: Params, onResult: (type: Type) -> Unit = {}) {
        val job = GlobalScope.async(Dispatchers.IO) { run(params) }
        GlobalScope.launch(Dispatchers.Main) { onResult(job.await()) }
    }
}

错误是get is

你能建议如何着手解决这个问题吗
谢谢,r

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题