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

前端 未结 3 1954
滥情空心
滥情空心 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:44

    adding this here since I couldn't find an example of iterating a list that is returned in an object as variable.

    getUserAccount(token)
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap(userResponse -> Observable.just(userResponse.list))      //get list from response
        .flatMapIterable(baseDatas -> baseDatas)                          //make the list iterable
        .flatMap(baseData ->                                              //on each project, get the details
                getProjectDetails(baseData.name,token)
                        .subscribeOn(Schedulers.io())                     //get network call off the main thread
                        .observeOn(AndroidSchedulers.mainThread()))
        .subscribe(
                (dataResponse) -> {
                    Timber.d( "Got Data Details:" + dataResponse);
                },
                (error) -> {
                    Timber.e( "Got Error:" + error.getMessage());
                },
                () -> {
                    Timber.d("Completed Data Details");
                }
        );
    

提交回复
热议问题