Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method Observable.from in rxjava 2

There is a from method in the Observable class in rxjava 1 but not found in rxjava 2. How can I replace the from method in rxjava 2 in the following code:

    List<Integer> ints = new ArrayList<>();
    for (int i=1; i<10; i++) {
        ints.add(new Integer(i));
    }
    Observable.just(ints)
            .flatMap(new Function<List<Integer>, Observable<Integer>>() {
                @Override
                public Observable<Integer> apply(List<Integer> ints) {
                    return Observable.from(ints);
                }
            })
like image 985
s-hunter Avatar asked Jan 12 '17 05:01

s-hunter


3 Answers

You can use Observable.fromIterable(source)

From documentation:

Some operator overloads have been renamed with a postfix, such as fromArray, fromIterable etc. The reason for this is that when the library is compiled with Java 8, the javac often can't disambiguate between functional interface types.

List<Integer> ints = new ArrayList<>();
for (int i=1; i<10; i++) {
    ints.add(new Integer(i));
}
Observable.just(ints)
        .flatMap(new Function<List<Integer>, Observable<Integer>>() {
            @Override
            public Observable<Integer> apply(List<Integer> ints) {
                return Observable.fromIterable(ints);
            }
        })
like image 99
Pavan Kumar Avatar answered Nov 15 '22 04:11

Pavan Kumar


You don't need to use .just() because you can create Observable directly from your list via fromIterable() operator.

    Observable.fromIterable(ints)
like image 40
Alexander Perfilyev Avatar answered Nov 15 '22 04:11

Alexander Perfilyev


I guess it's a bit late, but I just wanted to let people know, the API changes related to the from operator in the RxJava documentation:

from disambiguated into fromArray, fromIterable, fromFuture

like image 6
cesards Avatar answered Nov 15 '22 05:11

cesards