Reputation: 1
Is there a way to let a Single
emit data and when it does (or maybe when it completes) fire a Completable
with that data in an asynchronous way?
My question is pretty similar to this, but I'm trying to call the Completable
asynchronously.
Here's a trivial example of what I'm trying to achieve:
Single.just("abc")
.map(string -> {
myHeavyCompletable(string); //this should be async
return string;
});
[...]
private Completable myHeavyCompletable(String string) {
//heavy logic that could slow down the execution
}
I saw that subscribing to the Completable
inside the map
allowed me to somehow accomplish this, but then I wouldn't know how to dispose it in a proper way (as this method is not called inside an activity).
Any help would be really appreciated, thank you!
Upvotes: 0
Views: 935
Reputation: 376
Consider using flatMapCompletable
and subscribe
asynchronously on proper thread. This approach combines all the logic as a pipelined sequence, if it isn't what you want you may subscribe your Completables in map and return the disposable as result and collect them at the end and dispose as needed.
Upvotes: 3