Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 661
Sergey Zabelnikov Avatar asked Oct 10 '16 07:10

Sergey Zabelnikov


3 Answers

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

like image 146
Dominik Mičuta Avatar answered Nov 19 '22 10:11

Dominik Mičuta


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());
    }
});
like image 17
Steve Avatar answered Nov 19 '22 09:11

Steve


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>.

like image 5
Hadi Satrio Avatar answered Nov 19 '22 08:11

Hadi Satrio