天天看点

RxSwift 操作符 (debounce)debounce

debounce

ReactiveX:

only emit an item from an Observable if a particular timespan has passed without it emitting another item

在指定的时间内,只接受最新的数据。

let pb1 = PublishSubject<Int>()
pb1.debounce(, scheduler: MainScheduler.instance)
    .subscribe(onNext: { int in
        print("element:", int)
    })
    .disposed(by: bag)
pb1.onNext()
pb1.onNext()
pb1.onNext()
pb1.onNext()
pb1.onNext()
           

输出: element: 5

指定了两秒钟,所以在两秒钟以内,只接收了到了最新的

element: 5