前言
Learning a new technology stack is a bit like building a skyscraper. You’ve got to build a solid foundation before you can kiss the sky.
在文档中挺喜欢这句话的。意思自行体会翻译,哈哈~
IgnoreElements
该操作符可以忽略掉所有的all.next events
,只响应.completed or .error event
事件。如果你只关心Observable 在什么时候终止的话,那就可以使用 ignoreElements 操作符。
func testIgnoreElements() {
let subject = PublishSubject()
subject.onNext("111")
//第1次订阅subject
subject.ignoreElements().subscribe(onCompleted: {
print("onCompleted")
}) { (err) in
print("err:\(err)")
}
subject.onNext("222")
subject.onNext("333")
subject.onCompleted()
}
输出:
onCompleted
filter
很好理解筛选可观察序列的值
example(of: "filter") {
let disposeBag = DisposeBag()
// 1
Observable.of(1, 2, 3, 4, 5, 6)
// 2
.filter { integer in
integer % 2 == 0
}
// 3
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
}
skip,skipWhile,skipUntil
skip指跳过observable序列的前n个元素
而skipWhile的意思是指skip only skips elements up until the first element is let through, and then all remaining elements are allowed through.(个人理解是指skipwhile里的逻辑来判断的是否kip 直到 当有一个元素发送通过后,后面所有的都能通过。)
func testSkip()
{
Observable.of(1, 2, 3, 4, 5, 6)
.skip(3)
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
print("========================")
Observable.of(2, 2, 3, 4, 5, 6)
.skipWhile({ (value) -> Bool in
return value % 2 == 0
})
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
print("========================")
Observable.of(1, 2, 3, 4, 5, 6)
.skipWhile({ (value) -> Bool in
return value % 2 == 0
})
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
}
输出:
4
5
6
========================
3
4
5
6
========================
1
2
3
4
5
6
skipUntil
它默认会一直skip 直到skipUntil里的东西触发了才不会skip了。
func testSkipUntil()
{
let disposeBag = DisposeBag()
print("=========skipUntil==============")
let subject = PublishSubject()
let trigger = PublishSubject()
// subject.onNext("A")
subject
.skipUntil(trigger)
.subscribe(onNext: {
print($0) })
.disposed(by: disposeBag)
subject.onNext("B")
trigger.onNext("X")
subject.onNext("D")
subject.onNext("E")
}
输出:
=========skipUntil==============
D
E
take
take的操作与skip相反
takeWhile与skipWhile相反
func testTake()
{
Observable.of(1, 2, 3, 4, 5, 6)
.take(3)
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
print("===========takeWhile=============")
Observable.of(2, 2, 3, 4, 5, 6)
.takeWhile({ (value) -> Bool in
return value < 4
})
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
let disposeBag = DisposeBag()
print("=========takeUntil==============")
let subject = PublishSubject()
let trigger = PublishSubject()
subject
.takeUntil(trigger)
.subscribe(onNext: {
print($0) })
.disposed(by: disposeBag)
subject.onNext("B")
subject.onNext("D")
trigger.onNext("X")
subject.onNext("E")
}
输出:
1
2
3
===========takeWhile=============
2
2
3
=========takeUntil==============
B
D
distinctUntilChanged
distinctUntilChanged
去除连续重复的元素
func testDistinctUntilChanged() {
Observable.of(1,1, 2, 3,3,3, 4, 5,5, 6)
.distinctUntilChanged()
.subscribe(onNext: {
print($0) })
.disposed(by: DisposeBag())
}
输出:
1
2
3
4
5
6
===========takeWhile=============
总结
上述的操作符还挺多的,不过想着这些都是对observable序列的值的操作,比如全忽略IgnoreElements,筛选fillter,跳过skip, 连续去重distinctUntilChanged等。它们只所以这样设计是有各种应用场景的
。
写的测试RXSwiftStudy