目录:
RxSwift - 入门
RxSwift Observable Create
RxSwift Subject
RxSwift Combination Operators
RxSwift Transforming Operators
RxSwift Filterning and conditional operators
RxSwift Mathematical and aggregate operators
RxSwift Connectable Operators
RxSwift ErrorHandding Operators
RxSwift Debug Operators
Demo地址
上一节了解了一下Transforming Operators,现在我们进行看看还有哪些常用的过滤 Operators
filter
根据条件,过滤掉事件
example("filter") {
let bag = DisposeBag()
Observable.of(
"🐱", "🐰", "🐶",
"🐸", "🐱", "🐰",
"🐹", "🐸", "🐱")
.filter {
$0 == "🐱"
}
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
distinctUntilChanged
当前的事件值不等于上一次的事件值的时候才会发送,可以用来过滤重复请求
example("distinctUntilChanged") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.distinctUntilChanged()
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
elementAt
取指定下标的事件,下标从0开始
example("elementAt") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.elementAt(3)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
single 和 single { 条件 }
取第一次事件
example("single") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.single()
.subscribe(
onNext: { print($0) },
onError: { print($0) }
).disposed(by: bag)
}
example("single with conditions") {
let bag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single { $0 == "🐸" }
.subscribe { print($0) }
.disposed(by: bag)
Observable.of("🐱", "🐰", "🐶", "🐱", "🐰", "🐶")
.single { $0 == "🐰" }
.subscribe { print($0) }
.disposed(by: bag)
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single { $0 == "🔵" }
.subscribe { print($0) }
.disposed(by: bag)
}
take
取前N次的事件,N由用户输入
example("take") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.take(3)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
takeLast
取后N次的事件,N由用户输入
example("takeLast") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.takeLast(3)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
takeWhile
closure
中编写条件,取满足掉件的值,当遇到不满足的时候,Observable会调用completed
方法结束
example("takeWhile") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.takeWhile { $0 < 3 }
.subscribe(onNext: { print($0) },
onError: { print($0) },
onCompleted: { print("completed") })
.disposed(by: bag)
}
takeUntil
参数是一个Observable,之后参数中的Observable发送了
onNext
事件才会监听到自己的onNext
事件,如果参数中的Observable发生了onError
事件,则会释放资源disposed
example("takeUntil") {
let bag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.takeUntil(referenceSequence)
.subscribe { print($0) }
.disposed(by: bag)
sourceSequence.onNext("1")
sourceSequence.onNext("2")
sourceSequence.onNext("2")
referenceSequence.onNext("3")
sourceSequence.onNext("1")
sourceSequence.onNext("4")
sourceSequence.onNext("2")
}
Skip
跳过N次事件,N由用户输入
example("skip") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.skip(3)
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
skipWhile
跳过事件,通过
closure
的返回值(bool
)决定忽略哪些事件
example("skipWhile") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.skipWhile { $0 < 3 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
skipWhileWithIndex
和
skipWhile
类似,只是在closure中多了index
,但是此方法被标记为过期
example("skipWhileWithIndex") {
let bag = DisposeBag()
Observable.of(1, 2, 2, 3, 1, 4, 2)
.skipWhileWithIndex { element, index in
index < 3
}
.subscribe(onNext: { print($0) })
.disposed(by: bag)
}
skipUntil
和
takeUntil
类似,表示一直忽略,直到参数中的Observable发送了onNext
事件
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("1")
sourceSequence.onNext("2")
sourceSequence.onNext("2")
referenceSequence.onNext("3")
sourceSequence.onNext("1")
sourceSequence.onNext("4")
sourceSequence.onNext("2")
}