1

I am getting User from Room Database by returning Single<UserMinimal>. Once I get the data I want to show it through LiveData observing, and I am using LiveDataReactiveStreams.fromPublisher() method, but looks like when .setValue() is called, all attributes of the received user are null. Don't know if needs .subscribe() or whatever.

LiveData<UserMinimal> userLiveData = LiveDataReactiveStreams
                .fromPublisher(mRepository.getUser().toFlowable());
        user = new MediatorLiveData<>();
        user.addSource(userLiveData, minimalUser -> {
            user.postValue(minimalUser);
            user.removeSource(userLiveData);
        });


public Single<MenuRepository.UserMinimal> getUser(){
    return dataSource.getUser()
            .onErrorReturnItem(new MenuRepository.UserMinimal())
            .doOnError(t -> Timber.e(t))
            .subscribeOn(Schedulers.io());
}

Looks like the access to database is successfully working because if I use MutableLiveData instead of MeadiatorLiveData, it does work:

user = new MutableLiveData<>();
        disposable.add(mRepository.getUser().subscribe(new Consumer<UserMinimal>() {
            @Override
            public void accept(UserMinimal userMinimal) throws Throwable {
                user.postValue(userMinimal);
            }
        }));
mantc_sdr
  • 451
  • 3
  • 17
  • Perhaps 'onErrorReturn' was invoked because of some error and you get 'MenuRepository.UserMinimal()' object. Can you check if error occured there? Please note, that you should change the order of 'onErrorReturn' and 'doOnError'. 'onErrorReturn' will ignore the exception, and emit the item provided in the parameter. The error will not be propagated downstream. – Dominik Setniewski Jun 10 '21 at 22:26

0 Answers0