Use RxJava and Retrofit to iterate through list and augment results based on subqueries

前端 未结 3 2030
滥情空心
滥情空心 2021-02-08 07:07

I\'m using retrofit and I feel like rxjava (with retrolambda) would be a good fit for the following flow:

  1. get list of widgets (http)
  2. for each widget

3条回答
  •  不要未来只要你来
    2021-02-08 07:35

    You could insert doOnNext at certain points of the stream to add side-effects:

    apiService.getWidgets(token)
    .flatMapIterable(v -> v)
    .flatMap(w -> 
        apiService.getArticles(token, w.type)
        .flatMapIterable(a -> a)
        .doOnNext(a -> db.insert(a))
        .doOnNext(a -> {
             w.articleName = a.name;
             w.articleUrl = a.url;
        })
        .takeLast(1)
        .map(a -> w)
    )
    .toList()
    .subscribe(
        modifiedWidgets -> saveWidgets(modifiedWidgets),
        throwable -> processWidgetError(throwable)
    );
    

    Here is runnable example of this.

提交回复
热议问题