Combine params from two Observables to execute the third one

86 views Asked by At

I have two observable A & B, each of them will query data separately. Now I want to combine response data from both streams to do another query.

Here is the code which works:

Observable<App> appObservable = this.appRepository.getApp();
Observable<DeviceInfo> deviceInfoObservable = this.deviceRepository.getDeviceInfo();

Observable<HashMap<String, Object>> combineDataObservable = Observable.zip(appObservable, deviceInfoObservable, (app, deviceInfo) -> {
        HashMap<String, Object> keyVal = new HashMap<>();
        keyVal.put("myApp", app);
        keyVal.put("myDevice", deviceInfo);

        return keyVal;
    });


Observable<String> registerDataObservable = combineDataObservable.flatMap(keyVal -> {
        App app = (App) keyVal.get("myApp");
        DeviceInfo deviceInfo = (JmDeviceInfo) keyVal.get("myDevice");
        return appRepository.registerApp(app, deviceInfo);
    });

Is there any way that I do not need to use third observable (combinedDataObservable) for preparing the desired data that I want?

Thanks

0

There are 0 answers