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

相关推荐
cwzqf38 分钟前
Jectpack Compose项目组件代码分享(1):分页加载组件
android
@北海怪兽1 小时前
SQL常见函数整理 _ STRING_AGG()
android·数据库·sql
鹏晨互联2 小时前
【Compose vs XML:边框内外间距的实现对比】
android·xml
Android系统攻城狮2 小时前
Android tinyalsa深度解析之pcm_plugin_write调用流程与实战(一百七十九)
android·pcm·tinyalsa·android16·音频进阶·android音频进阶
ID_180079054733 小时前
除了JSON,淘宝店铺商品API接口还支持哪些数据格式?
android·数据库
KillerNoBlood3 小时前
2026移动端跨平台开发面经总结
android·算法·flutter·ios·移动开发·鸿蒙·kmp
消失的旧时光-19433 小时前
Android / IoT 面试复盘总结:从 MQTT、TLS 到 JWT 权限体系(标准答案 + 工程理解 + 延伸知识链)
android·物联网·面试
林多5 小时前
【Android】 GPU过度绘制实现原理
android·gpu·性能·实现原理·过度绘制·overdraw
薄荷椰果抹茶5 小时前
手机端Obsidian安装与同步全攻略
android
醇氧5 小时前
CentOS 7安装 mysql-8.0.27-1.el7.x86_64.rpm 安装包
android·mysql·centos