Null handling in rx-java2 flatMap

2019-08-16 17:11发布

As explained in the docs RxJava 2.x no longer accepts null values. So it is not surprising that both of the following two lines terminate with onError called:

Observable.fromCallable(() -> null);
Observable.just(1).flatMap(i -> Observable.error(new RuntimeException()));

what is unclear is why

Observable.just(1).flatMap(i -> Observable.fromCallable(() -> null))

terminates with success and no items emitted. It seams reasonable to expect for it behave in the same fashion as Observable.error

I can see in source code of rx-java 2.1.2

 public final <R> Observable<R> flatMap(...) {
    if (this instanceof ScalarCallable) {
        @SuppressWarnings("unchecked")
        T v = ((ScalarCallable<T>)this).call();
        if (v == null) {
            return empty();
        }
        ...
 }

which explains why it is happening in terms of code, but I still have two questions:

1) Is this is an intended behavior or a bug?

2) If intended, is there a reason for this?

1条回答
叼着烟拽天下
2楼-- · 2019-08-16 18:05

This is a bug with the Observable.fromCallable and will be fixed with PR 5517.

If, for some reason you can't avoid a null return in this setup, you can apply hide() to workaround this bug:

Observable.just(1).flatMap(i -> Observable.fromCallable(() -> null).hide())

or help RxJava throw:

Observable.just(1)
    .flatMap(i -> Observable.fromCallable(() -> 
         java.util.Objects.requireNonNull(apiReturningNull()))
    )
查看更多
登录 后发表回答