Search code examples
javaandroidrx-java

RxJava: map Single value to a set of Completable that run concurrently


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


Solution

  • 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 Completables. Something like

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