Null handling in rx-java2 flatMap -
as explained in docs rxjava 2.x no longer accepts null values. not surprising both of following 2 lines terminate onerror
called:
observable.fromcallable(() -> null); observable.just(1).flatmap(i -> observable.error(new runtimeexception()));
what unclear why
observable.just(1).flatmap(i -> observable.fromcallable(() -> null))
terminates success , no items emitted. seams reasonable expect behave in same fashion 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 happening in terms of code, still have 2 questions:
1) intended behavior or bug?
2) if intended, there reason this?
this bug observable.fromcallable
, fixed pr 5517.
if, reason can't avoid null return in setup, can apply hide()
workaround bug:
observable.just(1).flatmap(i -> observable.fromcallable(() -> null).hide())
or rxjava throw:
observable.just(1) .flatmap(i -> observable.fromcallable(() -> java.util.objects.requirenonnull(apireturningnull())) )
Comments
Post a Comment