Ios 每60秒观察一个可观测值,并将其与RxSwift中以前的值进行比较

Ios 每60秒观察一个可观测值,并将其与RxSwift中以前的值进行比较,ios,swift,reactive-programming,core-location,rx-swift,Ios,Swift,Reactive Programming,Core Location,Rx Swift,我想做的是: 每60秒观察一次可观察到的位置,并与约60秒前发出的事件相比,比较距离是否超过阈值 发生在$0中的是我总是得到第一个发出的事件,它不是每60秒更新一次$1具有最新发出的事件 代码如下: Observable<Int>.timer(.seconds(0), period: .seconds(60), scheduler: MainScheduler.instance) .withLatestFrom(location)

我想做的是:

  • 每60秒观察一次可观察到的
    位置
    ,并与约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)
Observable.timer(.seconds(0),period:.seconds(60),调度器:MainScheduler.instance)
.withLatestFrom(位置)
.distinctUntilChanged{$0.distance(from:$1)。震级<10.0}
.subscribe(onNext:{(位置)在中)
打印(位置)
})
.处置(由:处置人)

您要求的是,当设备以特定速度运行时,发出一个值,该值实际上是位置对象中提供的值。就用它吧

extension CLLocationManager {
    func goingFast(threshold: CLLocationSpeed) -> Observable<CLLocation> {
        return rx.didUpdateLocations
            .compactMap { $0.last }
            .filter { $0.speed > threshold }
    }
}
也就是说,作为跟踪幅度增加的一般情况,您需要使用
scan
操作符

extension CLLocationManager {
    func example(period: RxTimeInterval, threshold: Double, scheduler: SchedulerType) -> Observable<CLLocation> {
        return rx.didUpdateLocations
            .compactMap { $0.last }
            .sample(Observable<Int>.interval(period, scheduler: scheduler))
            .scan((CLLocation?.none, false)) { last, current in
                if (last.0?.distance(from: current).magnitude ?? 0) < threshold {
                    return (current, false)
                }
                else {
                    return (current, true)
                }
            }
            .filter { $0.1 }
            .compactMap { $0.0 }
    }
}
扩展CLLocationManager{
func示例(句点:rxtimeeval,阈值:Double,调度程序:SchedulerType)->可观察{
返回rx.didUpdateLocations
.compactMap{$0.last}
.sample(可观察的时间间隔(周期,调度程序:调度程序))
.scan((CLLocation?.none,false)){last,current in
如果(最后0°。距离(从:电流)。幅值??0)<阈值{
返回(当前,错误)
}
否则{
返回(当前,真)
}
}
.filter{$0.1}
.compactMap{$0.0}
}
}

我想您正在寻找,虽然我不确定它与
最新from
@monica有何不同,但您可以共享一个RxSwift示例吗?不,我没有设置RxSwift工作区,我没有时间这么做。问题是,即使是一个位置的更新也会给出稍微不准确的更新,即0.167,逻辑就会中断。这就是我试图增加时间间隔的原因。我需要检查的是,如果一台设备在过去60秒内总共覆盖了1000万次电源更新,但即使有这种想法,如果一次(采样)位置更新给出的结果不准确,逻辑也会断开。
extension CLLocationManager {
    func example(period: RxTimeInterval, threshold: Double, scheduler: SchedulerType) -> Observable<CLLocation> {
        return rx.didUpdateLocations
            .compactMap { $0.last }
            .sample(Observable<Int>.interval(period, scheduler: scheduler))
            .scan((CLLocation?.none, false)) { last, current in
                if (last.0?.distance(from: current).magnitude ?? 0) < threshold {
                    return (current, false)
                }
                else {
                    return (current, true)
                }
            }
            .filter { $0.1 }
            .compactMap { $0.0 }
    }
}