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

相关推荐
TAEHENGV3 分钟前
进度跟踪模块 Cordova 与 OpenHarmony 混合开发实战
android·javascript·数据库
酸菜牛肉汤面24 分钟前
6、索引算法有哪些?
android
青春勿语28 分钟前
Lumen:重新定义 Android 图片加载体验
android·glide
TAEHENGV1 小时前
回收站模块 Cordova 与 OpenHarmony 混合开发实战
android·java·harmonyos
TAEHENGV2 小时前
创建目标模块 Cordova 与 OpenHarmony 混合开发实战
android·java·开发语言
zjw_swun3 小时前
Compose原理简易实现
android·composer
青莲8433 小时前
Kotlin Flow 深度探索与实践指南——中部:实战与应用篇
android·前端
建群新人小猿4 小时前
陀螺匠企业助手-我的日程
android·大数据·运维·开发语言·容器
_李小白4 小时前
【Android FrameWork】第三十九天:DeviceStorageManagerService
android
不急不躁1235 小时前
Android16 给应用默认获取权限
android·java