【问题标题】:LiveDataReactiveStreams: converting Flowable to LiveData doesn't workLiveDataReactiveStreams:将 Flowable 转换为 LiveData 不起作用
【发布时间】:2018-11-16 07:40:26
【问题描述】:

我正在尝试将 Flowable 转换为 LiveData 并在活动中观察它。我的 Flowable 以恒定延迟发出值, 但是我将此 Flowable 转换为的 LiveData 根本没有在其观察者中接收任何值。我创建了一个示例代码 演示问题

活动

    public class MyrActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            MyViewModel myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
            myViewModel.init();
            myViewModel.getListLiveData().observe(this, new Observer<List<String>>() {
                @Override
                public void onChanged(@Nullable List<String> strings) {
                    Timber.d("value received in live data observer: %s", strings);
                    // This callback never get called
                    for (String string : strings) {
                        Timber.d(string);
                    }
                }
            });

        }
    }

ViewModel 类

     static class MyViewModel extends ViewModel{
            LiveData<List<String>> mListLiveData;
            PublishProcessor<String> mStringPublishProcessor = PublishProcessor.create();

            public void init() {
                mListLiveData = LiveDataReactiveStreams.fromPublisher(mStringPublishProcessor.toList().toFlowable());

                // This is to trigger the mStringPublishProcessor on constant intervals
                Observable.interval(0,5,TimeUnit.SECONDS)
                        .map(aLong -> {
                            Timber.d("value emitted: ");  // this log is showing as expected
                            mStringPublishProcessor.onNext("Value "+aLong);
                            return aLong;
                        }).subscribe();
            }

            public LiveData<List<String>> getListLiveData() {
                return mListLiveData;
            }
        }

现在,在我的活动中,我只能看到来自 Observable.interval 的日志

     com.example.app D/MyActivity$MyViewModel: value emitted: 
     com.example.app D/MyActivity$MyViewModel: value emitted: 
     com.example.app D/MyActivity$MyViewModel: value emitted: 
     com.example.app D/MyActivity$MyViewModel: value emitted: 
     com.example.app D/MyActivity$MyViewModel: value emitted: 
     com.example.app D/MyActivity$MyViewModel: value emitted: 

为什么 LiveData 观察者从未收到来自 Flowable 的任何值?

根据 LiveDataReactiveStreams.fromPublisher 的文档

从 ReactiveStreams 发布者创建一个 Observable 流。 当 LiveData 变为活动状态时,它会订阅来自 Publisher 的发射。

【问题讨论】:

  • 如果这是 api 调用,为什么我们必须在 onNext 调用时使用它而不是正常使用 setValue
  • LiveDataReactiveStreams.fromPublisher 将为您做同样的事情。您不必订阅每个 observable 并手动将其转换为 LiveData。使用setValue(前提是您正确处理错误,因为LiveData 不处理错误,并且它希望错误被视为所保存数据中的状态)
  • 你有例子吗,我仍然对此感到困惑,tkanks
  • 我想你理解错了。它不是 Rx 的替代品。它只是一个实用类,有助于将 Rx Observable 转换为 LiveData,反之亦然
  • 详情请咨询Documentation

标签: android rx-java rx-java2 android-architecture-components android-livedata


【解决方案1】:

.toList() 将仅在 onComplete() 调用后映射返回。在您的示例中,从不调用完成。

【讨论】:

  • 哦,是的...我的来源是一个热门的 observable,它从不调用 onComplete 并且一直保持活跃。没注意到。谢谢!!
  • 我们可以在调用 api 时使用 LiveDataReactiveStreams 吗,你有这个例子吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多