RxJava组其中之一两种反应可能是NULL拉链运营商(RxJava group two respo

2019-10-29 06:33发布

我想提出两个concurent请求境界数据库,并RxJava返回结果。 问题是,请求人可以返回NULL对象,我有一个崩溃。 我怎样才能正确地从两个数据库返回的结果,即使其中的一个NULL? 谢谢。

public void getDataFromTwoSources(DisposableObserver<ComplexUserDto> observer, String shopId) {
        Preconditions.checkNotNull(observer);

        final Observable<ComplexUserDto> observable = Observable.zip(firstRepository.getUserData(userId), secondRepository.getUserDetailData(userId),
                (userData, userDetails) -> {
                    ComplexUserDto resultDto = new ComplexUserDto(userData, userDetails);
                    return resultDto;
                })
                .compose(rxTransformers.applyUnauthorizedHandler())
                .compose(rxTransformers.applySchedulers());
        addDisposable(observable.subscribeWith(observer));
    }

public class ComplexUserDto {
    private UserData userData;
    private UserDetailData userDetailData;

    public ComplexUserDto(UserData userData, UserDetailData userDetailData) {
        this.userData = userData;
        this.userDetailData = userDetailData;
    }

    public UserData getUserData() {
        return userData;
    }

    public UserDetailData getUserDetailData() {
        return userDetailData;
    }
}

例外:

java.lang.NullPointerException: onNext called with null. Null values are generally not allowed in 2.x operators and sources.
        at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:63)
        at com.yozhik.data.database.shop.RealmDatabaseManagerImpl.lambda$null$15$RealmDatabaseManagerImpl(RealmDatabaseManagerImpl.java:261)
        at com.yozhik.data.database.shop.RealmDatabaseManagerImpl$$Lambda$15.execute(Unknown Source)
        at io.realm.Realm$1.run(Realm.java:1502)
        at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

Answer 1:

您可以从数据库返回可选,然后检查它是否存在。



文章来源: RxJava group two responses one of which might be NULL with zip operator