简体   繁体   English

RxJava2使用Flowable检测零事件条件

[英]RxJava2 Detect zero event condition with Flowable

I am using an android library FrangSierra for listening to Firebase database events. 我正在使用一个Android库FrangSierra来监听Firebase数据库事件。 This library returns RxJava2 Flowables to work with. 这个库返回RxJava2 Flowables一起工作。 I show a loading dialog when I start loading data and need to hide it when data is received. 当我开始加载数据时显示一个加载对话框,并且在接收数据时需要将其隐藏。 Now the problem is when there is no data and I need to show an empty view because Flowable never calls OnComplete . 现在的问题是,当没有数据时,我需要显示一个空视图,因为Flowable从不调用OnComplete

I tried applying timeout and the problem is it will probably always timeout after data is finished loading. 我尝试应用timeout ,问题是数据加载完成后,它可能总是超时。 I also want to keep subscription for future events. 我也想保留订阅以备将来使用。 My question is how do I detect 0 records scenario here. 我的问题是如何在这里检测0条记录。

You can use switchIfEmpty operator: 您可以使用switchIfEmpty运算符:

Flowable.just(1)
        .filter(value -> (value != 1))
        .switchIfEmpty(Completable.fromRunnable(() -> System.out.println("Empty stream")).toFlowable())
        .subscribe();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM