ios - 如何观察属性在 RxSwift 中的特定时间间隔内是否没有变化
问题描述
我想观察speed
CLLocation 更新的属性,如果速度在 10 秒内没有变化并且速度变化会重置计时器,则触发一个事件。到现在也只能想出这么多代码。
let location: Observable<CLLocation>
location.subscribe(onNext: { (coordinates) in
print(coordinates)
})
.disposed(by: disposeBag)
我认为我们可以使用debounce
或throttle
不确定如何使用。
解决方案
试试这个:
// will emit element when speed doesn't change for 10 seconds
let lastLocation = location
.distinctUntilChanged { $0.speed == $1.speed }
.debounce(.seconds(10), scheduler: MainScheduler.instance)
Observable.merge(location, lastLocation)
.subscribe(onNext: { coordinates in
print(coordinates)
})
.disposed(by: disposeBag)
推荐阅读
- azure - 防止 Azure Live Metrics Stream 将每次轮询记录到队列中
- php - Php: Email received without any messages
- c++ - 传递可变参数时在成员初始化器列表中进行强制转换
- javascript - 同一阵列中的 OSM 标记和折线
- c# - 在派生接口中隐藏接口成员
- sql-server - Datareader 到 Datasource OK,但 Datagridview 只显示 1 行
- python - 获取动态生成的图片
- spring-cloud-stream - spring cloud stream 3.0 存在生产者问题
- wordpress - 我使用 SQL 查询添加 woocommerce 产品类别。但我认为蛞蝓在那里并不完美,因为它得到了 404
- button - 按下按钮时如何在一个标签中显示两个事件