【Android】Analysis of Empty Observable in RxJava

About Empty Observable

In rxjava, there is a function called Observable.empty()

This function return an object that is instance of ObservableEmpty

This class will emit a complete event instead of next event, then rx chain will ended

Empty Observable with Map Operator

Now let's test what will happen, when ObservableEmpty is not top level observables

I means it was mapped from other observables by operator such as flatMap

Trial Code
kotlin 复制代码
Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
    .flatMap {
        if (it != 5) {
            return@flatMap Observable.just(it.toString())
        }
        return@flatMap Observable.empty<String>()
    }
    .doOnNext { println(it) }
    .subscribe()

We get this result

shell 复制代码
1
2
3
4
6
7
8
9

When it's a mediator observable, it won't interrupt all top observables

It just break the sub event stream that related to itself

Empty Observable with ConcatArray Operator

This time, we make it the top ones, but not the single one

We combine it with other normal observables by concatArray , as top level observable in total

kotlin 复制代码
Observable.concatArray(
    Observable.just(1),
    Observable.just(2),
    Observable.just(3),
    Observable.empty(),
    Observable.just(4),
    Observable.just(5)
).flatMap {
    if (it != 5) {
        return@flatMap Observable.just(it.toString())
    }
    return@flatMap Observable.empty<String>()
}
    .doOnNext { println(it) }
    .subscribe()

This is the output

shell 复制代码
1
2
3
4

We can see that, ObservableEmpty interrupt other observables in the same level

Also, it interrupt the whole rx chain, directlly jump to onComplete

Summary
  • ObservableEmpty will interrupt datas or observables mapped from it

  • ObservableEmpty will interrupt observables at same level, but after it in order

Bless

Lesson is Over, Have A Rest, and Enjoy Your Life .

Good Work, Good Study, Good Progress, and Good Mood !

相关推荐
Answer_momo1 小时前
一文读懂 Kotlin 数据流 Flow 的使用
android
雨白1 小时前
Kotlin Flow 入门:构建响应式异步数据流
android·kotlin
阿里云云原生2 小时前
告别手动埋点!Android 无侵入式数据采集方案深度解析
android·云原生
Tlaster2 小时前
使用KMP实现原生UI + Compose混合的社交客户端
android·ios·开源
袁煦丞 cpolar内网穿透实验室3 小时前
安卓旧机变服务器,KSWEB部署Typecho博客并实现远程访问:cpolar内网穿透实验室第645个成功挑战
android·运维·服务器·远程工作·内网穿透·cpolar
游戏开发爱好者83 小时前
iOS 26 App 查看电池寿命技巧,多工具组合实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
用户41659673693553 小时前
基于Jetpack Compose 实现列表嵌套滚动联动机制 (完整源码解析)
android
林栩link3 小时前
【车载Android】使用自定义插件实现多语言自动化适配
android
消失的旧时光-19438 小时前
Flutter 响应式 + Clean Architecture / MVU 模式 实战指南
android·flutter·架构
404未精通的狗8 小时前
(数据结构)栈和队列
android·数据结构