7. RxSwift 订阅操作过滤2 distinctUntilChanged、single、elementAt、ignoreElements、take、takeLast、skip、debounce
2021-03-14 19:33
标签:nts tun shu glob ext 例子 abs second getc 过滤连续重复的事件 只发送一次正常事件,如果没有或者超过 1 个会发送 获取指定位置的事件,0 开始。 如果没有发生该指定位置事件 会发送错误 会忽略所有主动发送的 只取前 n 个事件,数量达到或不足会发送 只取后 n 个事件,数量达到或不足会发送 跳过前 n 个事件,发送完剩下的事件后会发送 当两个事件的发送间隔大于约定时间时才会收到该事件,常用的例子是搜索时延迟搜索 搬迁自本人简书 7. RxSwift 订阅操作过滤2 distinctUntilChanged、single、elementAt、ignoreElements、take、takeLast、skip、debounce 标签:nts tun shu glob ext 例子 abs second getc 原文地址:https://www.cnblogs.com/i2yf/p/14016624.htmldistinctUntilChanged
let ob = Observable.of(1, 1, 1, 3, 5, 7, 9, 9)
ob.distinctUntilChanged().subscribe(onNext: { element in
print(element)
//1 3 5 7 9
}).disposed(by: disposeBag)
single
error
事件let ob = Observable.of(1,2)
ob.single().subscribe(onNext: { element in
print(element)
}, onError: { error in
print("错误",error)
}).disposed(by: disposeBag)
// 1
// 错误 Sequence contains more than one element.
//----- single 增加过滤
let ob = Observable.of(1, 2, 1)
ob.single { $0 == 2 }.subscribe(onNext: { element in
print(element)
}, onError: { error in
print("错误", error)
}).disposed(by: disposeBag)
// 2
elementAt
let ob = Observable.of(0, 1, 2, 3, 4)
ob.elementAt(2).subscribe(onNext: { element in
print(element)
}, onError: { error in
print(error)
}).disposed(by: disposeBag)
//2
ignoreElements
event
事件,只保留 error
事件和 Completed
事件let ob = Observable.of(0,1,2,3,4)
ob.ignoreElements().subscribe {
print("Completed")
} onError: { (error) in
print(error)
}.disposed(by: disposeBag)
take
Completed
。let ob = Observable.of(0, 1, 2, 3)
ob.take(2).subscribe { element in
print(element)
} onError: { error in
print(error)
} onCompleted: {
print("onCompleted")
}.disposed(by: disposeBag)
takeLast
Completed
。let ob = Observable.of(0, 1, 2, 3, 4)
ob.takeLast(12).subscribe { element in
print(element)
} onError: { error in
print(error)
} onCompleted: {
print("onCompleted")
}.disposed(by: disposeBag)
skip
onCompleted
。不足 n 个直接发送onCompleted
。let ob = Observable.of(0,1,2,3,4,5)
ob.skip(4).subscribe { (element) in
print(element)
} onError: { (error) in
print(error)
} onCompleted: {
print("onCompleted")
}.disposed(by: disposeBag)
debounce
let ob = PublishSubject
文章标题:7. RxSwift 订阅操作过滤2 distinctUntilChanged、single、elementAt、ignoreElements、take、takeLast、skip、debounce
文章链接:http://soscw.com/index.php/essay/64676.html