I am using Helidon DBClient transactions and have found myself in a situation where I end up with a list of Singles, List<Single<T>>
and want to perform the next task only after completing all of the singles.
I am looking for something of equivalent to CompletableFuture.allOf() but with Single.
I could map each of the single toCompletableFuture() and then do a CompletableFuture.allOf() on top, but is there a better way? Could someone point me in the right direction with this?
--
Why did I end up with a List<Single>?
I have a collection of POJOs which I turn into named insert .execute() all within an open transaction. Since I .stream() the original collection and perform inserts using the .map() operator, I end up with a List when I terminate the stream to collect a List. None of the inserts might have actually been executed. At this point, I want to wait until all of the Singles have been completed before I proceed to the next stage.
This is something I would naturally do with a CompletableFuture.allOf(), but I do not want to change the API dialect for just this and stick to Single/Multi.
Single.flatMap
,Single.flatMapSingle
,Multi.flatMap
will effectively inline the future represented by the publisher passed as argument.You can convert a
List<Single<T>>
toSingle<List<T>>
like this:Things can be tricky when you are dealing with
Single<Void>
as Void cannot be instantiated andnull
is not a valid value (i.e.Single.just(null)
throws aNullPointerException
).