【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 !

相关推荐
流星白龙1 小时前
【MySQL】7.MySQL基本查询(2)
android·mysql·adb
mldlds1 小时前
MySQL加减间隔时间函数DATE_ADD和DATE_SUB的详解
android·数据库·mysql
智算菩萨3 小时前
MP3音频编码原理深度解析与Python全参数调优实战:从心理声学模型到LAME编码器精细控制
android·python·音视频
studyForMokey5 小时前
【Android面试】Activity生命周期专题
android·面试·职场和发展
chehaoman5 小时前
MySQL的索引
android·数据库·mysql
恋猫de小郭8 小时前
React Native 鸿蒙 2026 路线发布,为什么它的适配成本那么高?
android·前端·react native
studyForMokey9 小时前
【Android面试】窗口机制专题
android·面试·职场和发展
用户0132014360310 小时前
Android 资源管理与常用布局详解|基础入门
android
陆业聪10 小时前
从 OpenClaw 到 Android:Harness Engineering 是怎么让 Agent 变得可用的
android·人工智能·ai编程
stevenzqzq12 小时前
颜色透明度转换技术文档(Android/Compose)
android