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

相关推荐
sTone87375几秒前
跨端框架通信机制全解析:从 URL Schema 到 JSI 到 Platform Channel
android·前端
sTone873752 分钟前
Java 注解完全指南:从 "这是什么" 到 "自己写一个"
android·前端
catoop15 分钟前
Kotlin 协程在 Android 开发中的应用:定义、优势与对比
android·kotlin
撒旦物种17 分钟前
Android WebView 获取内容高度
android·webview
空中海37 分钟前
第七章:安卓性能优化
android·性能优化
空中海1 小时前
第四章:导航与路由
android
2501_916007473 小时前
iOS逆向工程:详细解析ptrace反调试机制的破解方法与实战步骤
android·macos·ios·小程序·uni-app·cocoa·iphone
空中海3 小时前
第三章:状态管理与 Jetpack 架构组件
android·架构
峥嵘life3 小时前
Android + Kiro AI软件开发实战教程
android·后端·学习
流星雨在线3 小时前
安卓路由技术选型调研
android·therouter·arouter