I have a bunch of observables that I'm running/subscribing to in a method (about 9 to be exact), that itself returns an observable. I narrowed it down to 2 observables for the purpose of this question. Here is my method that I created that returns an Observable that contains the other observables.
public static Observable<MyCustomObject> runAll(Service networkService) {
return Observable.create(subscriber -> {
networkService.getOne().subscribe(response -> {
Request request = response.raw().request();
MyCustomObject case = new MyCustomObject(request);
subscriber.onNext(case);
}, exception -> {
throw new OnErrorNotImplementedException(exception);
});
networkService.getTwo().subscribe(response -> {
Request request = response.raw().request();
MyCustomObject case = new MyCustomObject(request);
subscriber.onNext(case);
}, exception -> {
throw new OnErrorNotImplementedException(exception);
});
subscriber.onComplete();
});
}
I then use the Observable that's returned...
runAll(networkService)
.subscribeOn(Schedulers.io())
.subscribe(case -> {
//do stuff
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//handle error
}
});
I'm not sure if I'm creating an observable correctly. I'm essentially replacing a listener/interface that I had here before with an Observable, but I know using Observable.create() is something that is not easy. Should I do it another way? Is there anything wrong with doing what I'm doing?
EDIT: getOne() and getTwo() are network calls so they return Observables.
EDIT 2: Currently have this
public static Observable<MyCustomObject> quickRun(Service networkService) {
return Observable.concat(
networkService.getOne().map(response -> {
Request request = response.raw().request();
MyCustomObject case = new MyCustomObject(request);
return case;
}),
networkService.getTwo().map(response -> {
Request request = response.raw().request();
MyCustomObject case = new MyCustomObject(request);
return case;
})
);
}