LiveDataReactiveStreams 到 MutableLiveData

LiveDataReactiveStreams to MutableLiveData

如何将值从 LiveDataReactiveStreams 发布到 MutableLiveData?我想实现到 Switch(视图)的双向数据绑定,并将 "checked" 值从数据库传递到 MutableLiveData 以及从 UI 传递。 LiveDAtaReactiveStreams returns 仅不可变 LiveData。

//ViewModel
public final MutableLiveData<Boolean> switchChecked = new MutableLiveData<>();

LiveData<Boolean> data = LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */); //??


//xml
<Switch
  ...
  android:checked="@={viewModel.switchChecked}"
/>

尝试MediatorLiveData

//ViewModel
public final MediatorLiveData<Boolean> switchChecked = new MediatorLiveData<>();

public MyViewModel() {
    ...

    switchChecked.addSource(LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */), value -> {
        switchChecked.setValue(value);
    });

    ...
}