RxJava2操作符之“Last”
2017-03-31 本文已影响38人
坑吭吭
作用
emits only the last item emitted by the source ObservableSource, or a default item if the source ObservableSource is empty
仅发出源Observable的最后一个项目,如果源Observable为空或者被订阅的时候已经完成发送了,则发送默认item
示例用法
Observable.just("A1", "A2", "A3", "A4", "A5", "A6")
.last("A1")
// Run on a background thread
.subscribeOn(Schedulers.io())
// Be notified on the main thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver());//这里的观察者依然不重要
运行结果
“A6”
分析
我们创建了一个会发送多个item的被观察者,
然后用操作符last,并设置一个默认值A1
最后,由于我们的观察者是从一开始就绑定的,所以能观察到整个的被观察者
所以从被观察者里的数据看,最后一个为A6
总结
这个系列只有干货,如果大家有什么好的建议的话欢迎在下面评论。或者觉得我哪里写的不够形象了,同样可以提出来。