Reputation: 36068
I have two operation:
I know how to use rx for each single operation, but once call all of them I only think about nest observable inside other, this will result in callback hell.
What's the right way to complete these jobs?
Upvotes: 0
Views: 509
Reputation: 10267
According to your description, Server and local DB query should happen in parallel, after we have both data we combined them, so you need to use zip operator. zip will subscribe to both server and local DB observable, when both Observable emitted values, you'll will get on next with both server and DB data, then combine them in the zip operator func, and you'll get Observable that emits the combined data.
With each emission of combined data (doOnNext), start a save operation in the background, and in the subscriber update the Ui according to the combined data.
Observable<ServerData> getServerData = ...;
Observable<LocalDbData> getLocalDbData = ...;
Observable
.zip(getServerData, getLocalDbData,
(serverData, localDbData) -> combinedData(serverData, localDbData))
.doOnNext(combinedData -> updateDataInDb())
.subscribe(combinedData -> updateUi(combinedData));
Upvotes: 1