I am trying to fetch a list of strings from API_1 (getList()
), then iterate through each string in the list and make another API_2(syncData()
) network call by each datatype string, somehow I don't understand why the code below can make second calls with forEach()
but not flatMap()
.
flatMap() Version:
Observable.just(getListNetworkCall())
.flatMapIterable(response -> response.getDataList())
.flatMap(dataType -> syncData(dataType))
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<Response2>() {
@Override
public void accept(Response2 response) throws Throwable {
System.out.println("response:" + response.toString());
}
});
forEach() Version:
Observable.just(getList())
.flatMapIterable(response -> response.getDataList())
.forEach(dataType -> syncData(dataType));
private Observable<Response2> syncData(String dataType) {
return Observable.just(syncDataNetworkCall(dataType));
}
What I saw in the flatMap() Version
is that it did the getList() call only. Am I missing any piece of knowledge on RxJava, I also take reference on RxJava Combine Sequence Of Requests
Thanks.