RxJava2找不到Func0

Ser*_*kov 7 android rx-java

RxJava 2几乎已经发布,我想从RX 1.2.0迁移到2.0.0,但我注意到RxJava 2中没有Func0接口.

开发人员应该在RxJava 2中使用哪些代替Func0?

Ste*_*eve 17

RxJava2使用JDK的Callable接口(https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html)

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());
    }
});
Run Code Online (Sandbox Code Playgroud)


Had*_*rio 5

根据他们对RxJava 1.x和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>.