我想做的是:
location
观测值,并与〜60秒前发出的事件进行比较,比较距离是否超过阈值$0
中发生了什么,我总是收到第一个发出的事件,它不是每60秒更新一次。 $ 1不过具有最近发出的事件。
这里是代码:
Observable<Int>.timer(.seconds(0), period: .seconds(60), scheduler: MainScheduler.instance)
.withLatestFrom(location)
.distinctUntilChanged { $0.distance(from: $1).magnitude < 10.0 }
.subscribe(onNext: { (location) in
print(location)
})
.disposed(by: disposeBag)
extension CLLocationManager {
func goingFast(threshold: CLLocationSpeed) -> Observable<CLLocation> {
return rx.didUpdateLocations
.compactMap { $0.last }
.filter { $0.speed > threshold }
}
}
也就是说,作为跟踪幅度增加的一般情况,您需要使用scan
运算符。
extension ObservableType where Element == Int { func example(threshold: Int) -> Observable<Int> { return scan((0, false)) { last, current in if current > last.0 + threshold { return (current, true) } else { return (current, false) } } .filter { $0.1 } .map { $0.0 } } }