码迷,mamicode.com
首页 > 编程语言 > 详细

7. RxSwift 订阅操作过滤2 distinctUntilChanged、single、elementAt、ignoreElements、take、takeLast、skip、debounce

时间:2020-11-25 12:36:02      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:nts   tun   shu   glob   ext   例子   abs   second   getc   

distinctUntilChanged

过滤连续重复的事件

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

只发送一次正常事件,如果没有或者超过 1 个会发送 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

获取指定位置的事件,0 开始。 如果没有发生该指定位置事件 会发送错误

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

只取前 n 个事件,数量达到或不足会发送 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

只取后 n 个事件,数量达到或不足会发送 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

跳过前 n 个事件,发送完剩下的事件后会发送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<String>()
ob.debounce(RxTimeInterval.seconds(1), scheduler: MainScheduler.instance).subscribe(onNext: { element in
    print("收", element, CFAbsoluteTimeGetCurrent())
}).disposed(by: disposeBag)
print("发", "aaa", CFAbsoluteTimeGetCurrent())
ob.onNext("aaa")
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
    print("发", "bbb", CFAbsoluteTimeGetCurrent())
    ob.onNext("bbb")
}
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
    print("发", "ccc", CFAbsoluteTimeGetCurrent())
    ob.onNext("ccc")
}
DispatchQueue.global().asyncAfter(deadline: .now() + 3.5) {
    print("发", "ddd", CFAbsoluteTimeGetCurrent())
    ob.onNext("ddd")
}

//发 aaa 627481044.719082
//发 bbb 627481045.241852
//发 ccc 627481046.231139
//收 ccc 627481047.233378
//发 ddd 627481048.552644
//收 ddd 627481049.55595

搬迁自本人简书

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.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!