RxJava:将单个值映射到一组并发运行的Completable。

huangapple go评论78阅读模式
英文:

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&lt;@NonNull ? extends CompletableSource&gt; sources) {
    ...  
}

so you need to pass a list of Completables. Something like

Completable.merge(
    Arrays.asList(
        auth.updateProfile(response, updates),
        auth.sendEmail(response),
        db.updateUser(response, user)
    )
)

huangapple
  • 本文由 发表于 2020年10月20日 21:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定