I'm using retrofit and I feel like rxjava (with retrolambda) would be a good fit for the following flow:
- get list of widgets (http)
for each widget
a) get a list of articles (http) for the given widget type
b) save all those to db
c) take the first (latest) article in list and update widget.articleName and widget.articleUrl with appropriate values from this article- transform back to list and complete
However I'm unsure what to do after step 2a. Here's my code so far
apiService.getWidgets(token)
.flatMapIterable(widgets -> widgets)
.flatMap(widget -> apiService.getArticles(token, widget.type))
...
.toList()
.subscribe(
modifiedWidgets -> saveWidgets(modifiedWidgets),
throwable -> processWidgetError(throwable)
);
I've played around with some operators but when chaining, I always seem to narrow down too far (e.g. get a handle on a single article) and then no longer have access to the original widget to make modifications.
@GET("/widgets")
Observable<List<Widget>> getWidgets(@Header("Authorization") String token);
@GET("/articles")
Observable<List<Article>> getArticles(@Header("Authorization") String token, @Query("type") String type);
adding this here since I couldn't find an example of iterating a list that is returned in an object as variable.
You could insert doOnNext at certain points of the stream to add side-effects:
Here is runnable example of this.
akarnokd's answer is quite helpful but that may cause
NetworkOnMainThreadException
. To solve that I have addedon every requests