gpt4 book ai didi

android - FlatMapCompletable 不会继续 Rx 链,但是带有 "andThen(Observable.just(true)"的可完成平面图可以工作吗?

转载 作者:行者123 更新时间:2023-12-04 23:49:17 27 4
gpt4 key购买 nike

我试图将一个可完成链接到我的 Rx 链中,当我这样做时,链永远不会在 onError 或 onComplete 中完成。

当我单步执行代码时,会执行我的可完成代码。我什至可以添加日志记录并查看它登录它自己的 doOnComplete()

下面将记录“我已完成”,但不会进入错误或完成回调。

 profileRepo.getLocalProfileIfAvailableElseRemote()
.flatMapCompletable { profile ->
userRoutingRepo.disableRule(profile.account_uid, userRoutingRule.id)
.doOnComplete {
Log.i("I COMPLETED", "I COMPLETED")
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onError = { error ->
//do error
},
onComplete = {
//do success
}
).addTo(disposable)

如果我改为使用 flatMap 并使用 andThen 返回一个 bool 可观察对象,它将起作用
 profileRepo.getLocalProfileIfAvailableElseRemote()
.flatMap { profile ->
userRoutingRepo.disableRule(profile.account_uid, userRoutingRule.id)
.doOnComplete {
Log.i("I COMPLETED", "I COMPLETED")
}.andThen(Observable.just(true))
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onError = { error ->
//do error
},
onNext = {
//do next
}
).addTo(disposable)

我尝试在 flatMapCompletable 版本中添加“andThen”并调用 Completable.complete() 但这也不起作用?

我不知道为什么我的可完成任务正在完成,但拒绝使用 flatMapCompletable?

编辑:这是我的完整尝试的更新,但不起作用

备注 userRoutingService.disableRule(accountUid, ruleId)是改造界面
 profileRepo.getLocalProfileIfAvailableElseRemote()
.flatMapCompletable { profile ->
userRoutingRepo.disableRule(profile.account_uid, userRoutingRule.id)
.andThen(Completable.complete())
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onError = { error ->
Log.i("TAG", "ERROR")
},
onComplete = {
Log.i("TAG", "COMPLETE")
}
).addTo(disposable)

override fun disableRule(accountUid: String, ruleId: String): Completable {
return activeStateToggler(userRoutingSourceApi.disableRule(accountUid, ruleId),
ruleId,
false)
}



override fun disableRule(accountUid: String, ruleId: String): Completable {
return userRoutingService.disableRule(accountUid, ruleId)
.doOnError { error ->
authenticationValidator.handleAuthenticationExceptions(error)
}
}

private fun activeStateToggler(completable: Completable,
ruleId: String,
stateOnSuccess: Boolean
): Completable {
return completable
.doOnSubscribe {
stateTogglingInProgress.add(ruleId)
}
.doOnComplete {
stateTogglingInProgress.remove(ruleId)
getLocalUserRule(ruleId)?.active = stateOnSuccess
stateTogglingInProgressPublishSubject.onNext(UserRoutingStateToggleSubjectType.Success)
}
.doOnError {
stateTogglingInProgress.remove(ruleId)
stateTogglingInProgressPublishSubject.onNext(UserRoutingStateToggleSubjectType.Error(
it))
}
}

最佳答案

这就是 flatMapCompletable 所做的:

Maps each element of the upstream Observable into CompletableSources,subscribes to them and waits until the upstream and allCompletableSources complete.


使用 flatMapCompletable 时,返回的 Completable 会等待上游的 Observable 终端事件(onComplete)。
使用 flatMapCompletable 时,仅当您确定链中的所有内容都已完成时才使用它。
在您的情况下,它不起作用,因为您的源 Observable 很热并且永远不会完成。

关于android - FlatMapCompletable 不会继续 Rx 链,但是带有 "andThen(Observable.just(true)"的可完成平面图可以工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226848/

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