location.filter({$0.speed < 25})
.debounce(.seconds(20), scheduler: MainScheduler.instance)
.subscribe(onNext: { (location) in
print(location)
}).disposed(by: disposeBag)
目标:
25 for 20 seconds
,则打印位置within 20
秒的速度达到above 25
,则取消发出的事件当前问题是:如果速度低于25,并且在20秒之内可观察到的第二个事件的速度低于25,则由于debounce
,它会取消上一个事件。
您应该添加distinctUntilChanged运算符:
location.distinctUntilChanged { $0.speed < 25 && $1.speed < 25 }
.debounce(.seconds(20), scheduler: MainScheduler.instance)
.filter { $0.speed < 25 }
.subscribe(onNext: { location in
print(location)
})
.disposed(by: disposeBag)