RxJava - Combining multiple/different web service calls

前端 未结 1 1848
眼角桃花
眼角桃花 2021-01-21 05:50

I\'m working with the Basecamp api to return and display to do lists. Here\'s a sample of what I\'m doing at the moment:

bcxClient
  .fetchToDoLists()
  .subscri         


        
相关标签:
1条回答
  • 2021-01-21 06:19

    You should not "break the chain" in your RxJava calls, hence you will need flatMap to chain them. Also Observable.from() lets you split List of elements into separate Observables. Next, you can use toList to get elements back into list. Here's an example :

    bcxClient
            .fetchToDoLists()
            .flatMap(new Func1<List<BcxToDoList>, Observable<BcxToDoList>>() {
                @Override
                public Observable<BcxToDoList> call(List<BcxToDoList> bcxToDoLists) {
                    return Observable.from(bcxToDoLists);
                }
            })
            .flatMap(new Func1<BcxToDoList, Observable<List<BcxToDo>>>() {
                @Override
                public Observable<List<BcxToDo>> call(BcxToDoList bcxToDoList) {
                    return bcxClient
                            .fetchToDos(bcxToDoList.bucket.id);
                            .map(new Func1<List<BcxToDo>, BcxToDoList>() {
                                @Override
                                public BcxToDoList call(List<BcxToDo> bcxToDos) {
                                    bcxToDoList.toDos.addAll(bcxToDos);
                                    return bcxToDoList;
                                }
                            });
                }
            })
            .toList()
            .subscribe(...);
    
    0 讨论(0)
提交回复
热议问题