I'm learning Rx-java and came accross a small problem. I'm trying to get a list of Object and pass it to Observable.from() so I can work on it.
Problem : this list needs to be get on another thread (http calls inside), so Observable.from(getList()) gives me nothing.
I've tried things like :
Observable.create(new Observable.OnSubscribe>() { @Override public void call(Subscriber> subscriber) { subscriber.onNext(getList()); subscriber.onCompleted(); } }).Subscribe ...
But this subscribe on an Iterable, the onNext only passes the full list instead of every OIbject in the list. What am I missing ? How do I do that ? Thanks
Update
Here is what I'm trying to do (and how I use toSortedList) :
Observable.from(getList())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.toSortedList(new Func2<Object, Object, Integer>() {
@Override
public Integer call(Object left, Object right) {
if (left.equals(right))
return 0;
return left.getLabel(pm).compareToIgnoreCase(right.getLabel(pm));
}
})
.subscribe(new Observer<List<Object>>() {
//On next sends a List of objects I'm using
}
Problem is that getList() needs to be called on another Thread (and it returns an Iterable, not an Observable). Maybe I'm not using your first solution correctly dwursteisen.
UPDATE 2
Here's something I don't understand why it's not working :
Observable.just(0)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Func1<Integer, Observable<Object>>() {
@Override
public Observable<Object> call(Integer integer) {
return Observable.from(getList());
}
})
.toSortedList(new Func2<Object, Object, Integer>() {
@Override
public Integer call(Object left, Object right) {
if (left.equals(right))
return 0;
return left.getLabel(pm).compareToIgnoreCase(right.getLabel(pm));
}
})
.subscribe( ... )
Or with replacing the flatMap call by a subscribe and doing everything in the onNext, it still calls getList() on the mainThread ...