(*useful)标记:目前觉得有用的函数
//FIXME 标记:待补充
Filtering 从可观察序列中选择性地发射元素的操作符。
filter(*useful) 只会让符合条件的元素通过
let disposeBag = DisposeBag()
Observable.of(
"🐱", "🐰", "🐶",
"🐸", "🐱", "🐰",
"🐹", "🐸", "🐱")
.filter {
$0 == "🐱"
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印:
--- filter example ---
next(🐱)
next(🐱)
next(🐱)
completed
distinctUntilChanged:(*useful) 过滤掉(连续)重复的事件
example("distinctUntilChanged") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐷", "🐱", "🐱", "🐱", "🐵", "🐱")
.distinctUntilChanged()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
🐱
🐷
🐱
🐵
🐱
elementAt:(*useful) 只执行指定位置的emit
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.elementAt(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印:
🐸
single
只发出可观察序列发出的第一个元素(或满足条件的第一个元素),否则会发出error
example("single") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
example("single with conditions") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single { $0 == "🐸" }
.subscribe { print($0) }
.disposed(by: disposeBag)
Observable.of("🐱", "🐰", "🐶", "🐱", "🐰", "🐶")
.single { $0 == "🐰" }
.subscribe { print($0) }
.disposed(by: disposeBag)
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single { $0 == "🔵" }
.subscribe { print($0) }
.disposed(by: disposeBag)
}
打印:
--- single example ---
🐱
Unhandled error happened: Sequence contains more than one element.
subscription called from:
--- single with conditions example ---
next(🐸)
completed
next(🐰)
error(Sequence contains more than one element.) //这个跟上面Unhandled是一样的错误
error(Sequence doesn't contain any elements.)
take:(*useful) 只获取序列中的前n 个事件,在满足数量之后会自动 .Completed 。
example("take") {
let subscription = sequenceOf(1, 2, 3, 4, 5, 6)
.take(3)
.subscribe {
print($0)
}
}
打印:
--- take example ---
Next(1)
Next(2)
Next(3)
Completed
takeLast: 只获取序列中的后n个事件,在满足数量之后会自动 .Completed 。
example("takeLast") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.takeLast(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
🐸
🐷
🐵
takeWhile: 条件
example("takeWhile") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.takeWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
--- takeWhile example ---
1
2
3
takeUntil :(*useful)!!! 一直执行,直到期待的Observable sequence发出指令
example("takeUntil") {
let disposeBag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.takeUntil(referenceSequence)
.subscribe { print($0) }
.disposed(by: disposeBag)
sourceSequence.onNext("🐱")
sourceSequence.onNext("🐰")
sourceSequence.onNext("🐶")
referenceSequence.onNext("🔴") //---这里收到的期待的observable sequence的emit,所以下面的消息不回被接收---
sourceSequence.onNext("🐸")
sourceSequence.onNext("🐷")
sourceSequence.onNext("🐵")
}
打印:
next(🐱)
next(🐰)
next(🐶)
completed
skip: (*useful) 控制emit事件的个数(跳过N个
example("skip") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.skip(2)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
--- skip example ---
🐶
🐸
🐷
🐵
skipWhile:条件(跟takeWhile相反,这个是跳过满足condition的emit)
example("skipWhile") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.skipWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
--- skipWhile example ---
4
5
6
skipWhileWithIndex:类似skipWhile,condition:避过满足index的emit
example("skipWhileWithIndex") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.skipWhileWithIndex { element, index in
index < 3
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
打印:
--- skipWhileWithIndex example ---
🐸
🐷
🐵
skipUntil:(*useful)与takeUntil相反
example("skipUntil") {
let disposeBag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.skipUntil(referenceSequence)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
sourceSequence.onNext("🐱")
sourceSequence.onNext("🐰")
sourceSequence.onNext("🐶")
referenceSequence.onNext("🔴")//---这里收到的期待的observable sequence的emit,所以上面的消息不回被接收---
sourceSequence.onNext("🐸")
sourceSequence.onNext("🐷")
sourceSequence.onNext("🐵")
}
打印:
--- skipUntil example ---
🐸
🐷
🐵