【问题标题】:RxJava: map Single value to a set of Completable that run concurrentlyRxJava:将单个值映射到一组同时运行的 Completable
【发布时间】:2020-10-20 13:21:13
【问题描述】:

预期:给定一个 Single 和多个 Completable,返回一个 Completable。

结果:无法解析方法 '合并(io.reactivex.rxjava3.core.Completable, io.reactivex.rxjava3.core.Completable, io.reactivex.rxjava3.core.Completable)'

我目前正在尝试设置用于注册用户的流程。但是,我似乎找不到任何关于将 Single 的结果映射到一组可完成的文档。 这里的想法是在某个数据库中创建一个用户,检索响应数据并调用不同的 API。每当我尝试使用可完成的合并方法执行此操作时,它都会引发上述错误。

例如,我们有一个 Single 为用户 (createUser) 设置基本信息并返回用户或错误,我们有多个 Completable(s) (updateProfile、sendEmail、updateUser) 在 API 上执行某些操作并返回完成状态或错误。谁能解释为什么会发生这种情况?

我的尝试:

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());

【问题讨论】:

    标签: java android rx-java


    【解决方案1】:

    我现在没有 IDE,但快速检查 the RxJava3 code for Completable 表明

    @CheckReturnValue
    @NonNull
    @SchedulerSupport(SchedulerSupport.NONE)
    public static Completable merge(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) {
        ...  
    }
    

    所以你需要传递Completables 的列表。类似的东西

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

    【讨论】:

    • 我明白了。我不敢相信我错过了这个。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多