Sergey Zabelnikov
Sergey Zabelnikov

Reputation: 1955

RxJava2 could not find Func0

RxJava 2 almost released and I want to migrate from RX 1.2.0 to 2.0.0, But I have noticed that there is no Func0 interface in RxJava 2.

What developers should use instead of Func0 in RxJava 2?

Upvotes: 9

Views: 5140

Answers (3)

Steve
Steve

Reputation: 533

RxJava2 use Callable interface from JDK (https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html)

Example with Observable.defer (http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html#defer(java.util.concurrent.Callable) )

Observable.defer(new Callable<ObservableSource<MyObject>>() {
    @Override
    public ObservableSource<MyObject> call() throws Exception {
        return Observable.just(new MyObject());
    }
});

Upvotes: 17

Dominik Mičuta
Dominik Mičuta

Reputation: 367

RxJava2 uses java.util.concurrent.Callable from java7 instead of Func0

Upvotes: 4

Hadi Satrio
Hadi Satrio

Reputation: 428

From their elaboration on what's changed between RxJava 1.x and 2.x:

We followed the naming convention of Java 8 by defining io.reactivex.functions.Function and io.reactivex.functions.BiFunction, plus renaming Func3 - Func9 into Function3 - Function9 respectively. The FuncN is replaced by the Function<Object[], R> type declaration.

In addition, operators requiring a predicate no longer use Func1<T, Boolean> but have a separate, primitive-returning type of Predicate<T> (allows better inlining due to no autoboxing).

The io.reactivex.functions.Functions utility class offers common function sources and conversions to Function<Object[], R>.

Upvotes: 5

Related Questions