Reputation: 218
I am pretty new to RxJava
I have an Observable
private static rx.Observable<List<item>> getList() {
return rx.Observable.fromCallable(() -> {
call some internal methods
return list;
});
}
I have a caller method
public static void getListFromObservable() {
getList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( new Subscriber<List<item>>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable error) {}
@Override
public void onNext(List<item> i) {
for (item each : i) {
getSubdetails(each)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable error) { }
@Override
public void onNext(Boolean aBoolean) {}
});
}
This is another observable
private static rx.Observable<Boolean>> getSubdetails(item i) {
return rx.Observable.fromCallable(() -> {
call some internal methods
return true;
});
}
I do not want to use foreach in the OnNext, but I want to use Map or FlatMaps. Can someone help me on how to do this?
Upvotes: 0
Views: 1764
Reputation: 26
You can try something like this:
getListObservable()
...
.flatMap( listOfItems ->
return Observable.fromIterable(listOfItems)
.concatMap(item -> getDetailsObservable(item))
.toList()
.toObservable()
)
.doOnNext { detailedListOfItems ->
// Do Something with your detailed items
}
There is a good book for beginners in RxJava https://www.manning.com/books/rxjava-for-android-developers I am not shore about the architect in that book. Especially with ViewModel, but many cool operators are explained very intuitive and graphically.
Difference between map and flatMap i finally understand with this book.
Upvotes: 0
Reputation: 70017
For example:
getList()
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.flatMap(each -> getSubdetails(each))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* ... */);
Upvotes: 2