RxJava - Combining multiple/different web service calls

前端 未结 1 1852
眼角桃花
眼角桃花 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, Observable>() {
                @Override
                public Observable call(List bcxToDoLists) {
                    return Observable.from(bcxToDoLists);
                }
            })
            .flatMap(new Func1>>() {
                @Override
                public Observable> call(BcxToDoList bcxToDoList) {
                    return bcxClient
                            .fetchToDos(bcxToDoList.bucket.id);
                            .map(new Func1, BcxToDoList>() {
                                @Override
                                public BcxToDoList call(List bcxToDos) {
                                    bcxToDoList.toDos.addAll(bcxToDos);
                                    return bcxToDoList;
                                }
                            });
                }
            })
            .toList()
            .subscribe(...);
    

    0 讨论(0)
提交回复
热议问题