ios - Chaining dependent observables -
i need create dependent api calls second 1 needs value returned first one. first thing comes mind using flatmap
apimanager.shared .createreport(report: report) .flatmap { (report) -> observable<report> in return apimanager.shared.createreportstep(reportid: report.id) } createreport returns observable<report> after successfull call returns updated report model(with id), after need call api create report step, report.id needed.
everything looks , works fine code, problem comes when need after each of these steps(createreport , createreportstep). placed code in onnext block, called once, after both of steps completed.
is there way receive onnext signal after both steps? use this:
apimanager.shared .createreport(report: report) .concat(apimanager.shared.createreportstep(reportid: report.id)) which emmit 2 signals want, again updated report.id pass createreportstep?
if don't mind time component , need have access both report , returned createreportstep(reportid:), go creating tuple in flatmap's block
apimanager.shared .createreport(report: report) .flatmap { (report) -> observable<report> in return apimanager.shared.createreportstep(reportid: report.id) .map { (report, $0) } } the resulting observable contain both results in tuple.
if time component important, following
let report = apimanager.shared .createreport(report: report) .share() let reportstep = report.map { $0.id }.flatmap(apimanager.shared.createreportstep) observable.concat([report, reportstep]) here, important bit share call. ensure createreport performs work once, have 2 next events requested.
Comments
Post a Comment