fionbio
fionbio

Reputation: 3554

RxJava- loop through async tasks and wait for complete

I'm attempting to do some async work on n items, and wait for all items to complete before proceeding.

What do I put in the whatfunc? location to have subscribe's onComplete fire once?

Observable.range(0, n)
    .<whatfunc?>({ s ->  doAsyncWorkThatReturnsObservable(s) })
    .(other?)
    .subscribe({println "All complete"})

Thanks

Upvotes: 2

Views: 1572

Answers (1)

fionbio
fionbio

Reputation: 3554

flatMap

Be sure to provide closures to the correct arguments in subscribe. Example from link adjusted to fit your provided example:

Observable.range(0, n)
    .flatMap({n -> doAsyncWorkThatReturnsObservable(n)})
    .subscribe(
       { println(it); },                          // onNext
       { println("Error: " + it.getMessage()); }, // onError
       { println("Sequence complete"); }          // onCompleted
    );

Upvotes: 1

Related Questions