My Scenario is very similar to this Image:
Flow of the app will be like this:
- View needs to get updated.
- Create an observable using
RxAndroid
to fetch the data from cache / local file. - update the view.
- Make another network call using
Retrofit
andRxJava
to update the view again with new data coming from the web services. - Update the local file with the new data.
So, I am updating the view twice(One from local file and just after that through webservices)
How can I achieve the result using RxJava
and RxAndroid
? What I was thinking is
- Create an observable1 to get the data from local file system.
- In the
onNext
method ofobservable1
I can create anotherobservable2
. observable2.onNext()
I can update the local file. Now How will I update theview
with the updated data (loaded in the file)?
What would be the good approach?
Looks like concat or merge operator is what you are looking for
The difference between them is:
Then subscribe for mergedObservable and update your view in
onNext()
I wrote a blog post about exactly this same scenario. I used the merge operator (as suggested by sockeqwe) to address your points '2' and '4' in parallel, and doOnNext to address '5':
In my blog post I additionally used filter and Timestamp to skip updating the UI if the data is the same or if cache is empty (you didn't specify this but you will likely run into this issue as well).
Link to the post: https://medium.com/@murki/chaining-multiple-sources-with-rxjava-20eb6850e5d9