Use throttle Operator.
See here for throttle and debounce with similar functionality.
RxSwift Debounce and Throttle
throttle can be used to prevent repeated button presses, and debounce can be used to prevent overcalling APIs such as so-called incremental search.
Actually, it is convenient to create the following extension.
Reactive+Extenstions.swift
import RxCocoa
import RxSwift
public extension Reactive where Base: UIButton {
var throttledTap: ControlEvent<Void> {
return ControlEvent<Void>(events: tap
.throttle(.milliseconds(ContinuousTap.disableTapDuration), latest: false, scheduler: MainScheduler.instance))
}
}
public extension Reactive where Base: UIBarButtonItem {
var throttledTap: ControlEvent<()> {
return ControlEvent<()>(events: tap
.throttle(.milliseconds(ContinuousTap.disableTapDuration), latest: false, scheduler: MainScheduler.instance))
}
}
public extension Reactive where Base: UITableView {
var throttledItemSelected: ControlEvent<IndexPath> {
return ControlEvent<IndexPath>(events: itemSelected
.throttle(.milliseconds(ContinuousTap.disableTapDuration), latest: false, scheduler: MainScheduler.instance) )
}
}
public extension Reactive where Base: UICollectionView {
var throttledItemSelected: ControlEvent<IndexPath> {
return ControlEvent<IndexPath>(events: itemSelected
.throttle(.milliseconds(ContinuousTap.disableTapDuration), latest: false, scheduler: MainScheduler.instance) )
}
}
enum ContinuousTap {
/// Disable Tap Duration in Milliseconds
static let disableTapDuration: Int = 500
}