Is there a way to convert a Completable to a Single<Void>? I just had an instance where I was trying to do just that, and I got a java.lang.NullPointerException: The value supplied is null
If I'm reading the source correctly (https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/Completable.java#L2276), toSingle() doesn't allow a function to return a null value, so something like Completable.complete().toSingle(() -> null); won't execute successfully.
My workaround was Completable.complete().toSingle(() -> true).map(aBool -> null). Wondered if I was completely off base.
Edit: Perhaps this is enforcing the use of Completable in all cases since that's what Single<Void> essentially is?
A more specific example of why I was trying this: attempting to flatMap a Single or Observable to a Completable.
return getItemSingle
.flatMap(item -> store.removeItem(item.id).toSingle(() -> null)) // removeItem(id) returns a Completable
.toCompletable();
The more correct solution would be to add an overload of flatMap for Completable. The entire reason for creating Completeable was to avoid the awful looking Single
Single.flatMapCompletable shoud be available now.
In RxJava 1, Observable.flatMapCompletable still returns an Observable. I would expect that to return a Completable instead just like the others. Is there a specific reason for that?
Returning a different base type is the courtesy of 2.x. Since existing API couldn't be changed for 1.x and the general API style is expected to be consistent within the version, there was no push for such change.
In addition, the earliest PR I could find that introduces specific types dates mid-September 2016 and flatMapCompletable was included in late August the same year.
Thanks a lot for the detailed explanation. It's always amazing to see the history behind it. We have 1 and 2 together and will migrate my stream to Rx Java 2 to have the Completable as a return type.
Most helpful comment
Single.flatMapCompletableshoud be available now.