I have multiple functions that return an Observable<String>
. Each function execute command on the file system. I need to execute each function one after another and get the output of the function in the observable. At the end i want a single Observable<String>
that contains the output of all the functions in the order of functions calls
Individually, each function work as expected but i need to merge output correctly.
I have try Observable.concatArray(func1, func2, ... ) like this:
return Observable.concatArray(
func1(),
func2(),
func3(),
func4()
);
but this just preserve the sequence of the observable's events. Not the sequence of the functions. I means if func1 emit events A and A' et func2 emit B and B', i will have A->A'->B->B'. But func2 will start immediately after func1. This cause me problem are func1 need to be finished before func2 can start.
The first function generate directory on the file system via maven. So, a long duration task. The second, writes a file inside this directory. But concatArray launch the second immediately after the first. And the command fail because the directory does not exist at this time.
Is there a way to avoid something ugly like this :
Subject<String> result = PublishSubject.create();
Observable<String> func1Obs = funct1();
Observable<String> func2Obs = funct2();
func1Obs.subscribe(output -> result.onNext(output));
func1Obs.onDoComplete(() -> {
func2Obs.subscribe(output -> result.onNext(output);
}
return result;