我通过返回 Room
从 Single<UserMinimal>
数据库获取用户。获得数据后,我想通过 LiveData
观察显示它,并且我正在使用 LiveDataReactiveStreams.fromPublisher()
方法,但是看起来当调用 .setvalue()
时,接收到的用户的所有属性都为空。不知道是否需要 .subscribe()
或其他什么。
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());
}
看起来对数据库的访问是成功的,因为如果我使用 MutableLiveData 而不是 MeadiatorLiveData,它确实有效:
user = new MutableLiveData<>();
disposable.add(mRepository.getUser().subscribe(new Consumer<UserMinimal>() {
@Override
public void accept(UserMinimal userMinimal) throws Throwable {
user.postvalue(userMinimal);
}
}));