Reputation: 1955
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
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
Reputation: 367
RxJava2 uses java.util.concurrent.Callable
from java7 instead of Func0
Upvotes: 4
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
andio.reactivex.functions.BiFunction
, plus renamingFunc3
-Func9
intoFunction3
-Function9
respectively. TheFuncN
is replaced by theFunction<Object[], R>
type declaration.In addition, operators requiring a predicate no longer use
Func1<T, Boolean>
but have a separate, primitive-returning type ofPredicate<T>
(allows better inlining due to no autoboxing).The
io.reactivex.functions.Functions
utility class offers common function sources and conversions toFunction<Object[], R>
.
Upvotes: 5