I'm new to RxJava/RxAndroid but I'm stuck with my use case.
I try to iterate a List<A>
to convert each A
into an B
asynchronously (because it needs call to database) and my Observer
should be notified on every successful conversion (in onNext()
) to do some jobs on the main thread.
I successfully managed to convert the entire list and then received my List<B>
in onNext()
but I need to be notified at each item, not once all items are done.
I tried something with the operator from
but if I use from(List<A>)
my Observer
must receivied objects from the same type (A
and not B
).
Here is how my code to convert entire List<A>
to list<B>
What should I change here?
private List<A> listOfA;
private startConversion() {
Observer observer = new Observer<List<B>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(List<B> convertedItems) {
onConversionCompleted(convertedItems);
}
};
Observable<B> observervable = Observable.fromCallable(new Callable<List<B>>() {
@Override
public List<B> call() throws Exception {
return convertListOfAToListOfB(listOfA);
}
});
subscription = observable
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
PS: this is pseudo code.
Thank's for help !