英文:
RxJava: map Single value to a set of Completable that run concurrently
问题
预期:给定一个 Single 和多个 Completable,返回一个 Completable。
结果:无法解析方法
'merge(io.reactivex.rxjava3.core.Completable,
io.reactivex.rxjava3.core.Completable,
io.reactivex.rxjava3.core.Completable)'
我目前正在尝试设置一个流程来注册用户。然而,我似乎找不到任何关于如何将 Single 的结果映射到一组 Completable 的文档。
这里的想法是在某个数据库中创建一个用户,检索响应数据并调用不同的 API。但是,每当我尝试使用 Completable 的 merge 方法时,它就会抛出上述错误。
例如,我们有一个 Single,它设置用户的基本信息(createUser)并返回用户或错误,还有多个 Completable(updateProfile、sendEmail、updateUser),它们在 API 上执行柜台操作并返回已完成的状态或错误。有人可以解释这可能是为什么吗?
我的尝试:
auth.createUser(field1, field2)
.flatMapCompletable(response ->
Completable.merge(
// 错误发生在这里
auth.updateProfile(response, updates),
auth.sendEmail(response),
db.updateUser(response, user)
)
)
.subscribeOn(Schedulers.io());
英文:
Expected: Given one Single and multiple Completable, return a completable.
> Result: Cannot resolve method
> 'merge(io.reactivex.rxjava3.core.Completable,
> io.reactivex.rxjava3.core.Completable,
> io.reactivex.rxjava3.core.Completable)'
I'm currently trying to set up a flow for registering a user. However, I can't seem to find any documentation about mapping a Single's result to a set of completable.
The idea here is create a user in some database, retrieve the response data and make calls to different API's. Whenever I attempt to do this with the completable merge method, it throws the above error.
For instance, we have an Single which sets up the basic information for user (createUser) and returns the user or an error, and we have multiple Completable(s) (updateProfile, sendEmail, updateUser) which do something on the API and return a completed status or an error. Can anyone explain why this might be happening?
My attempt:
auth.createUser(field1, field2)
.flatMapCompletable(response ->
Completable.merge(
// Error occur here
auth.updateProfile(response, updates),
auth.sendEmail(response),
db.updateUser(response, user)
)
)
.subscribeOn(Schedulers.io());
答案1
得分: 1
我现在没有IDE,但是快速查看关于Completable
的RxJava3代码显示
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable merge(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) {
...
}
所以你需要传递一个Completable
列表。类似于
Completable.merge(
Arrays.asList(
auth.updateProfile(response, updates),
auth.sendEmail(response),
db.updateUser(response, user)
)
)
英文:
I don't have an IDE right now, but a quick check on the RxJava3 code for Completable
shows that
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable merge(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) {
...
}
so you need to pass a list of Completable
s. Something like
Completable.merge(
Arrays.asList(
auth.updateProfile(response, updates),
auth.sendEmail(response),
db.updateUser(response, user)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论