哪个RxSwift运算符用于唯一发射的元素,以及如何使用?

问题描述 投票:0回答:1
location.filter({$0.speed < 25})
.debounce(.seconds(20), scheduler: MainScheduler.instance)
.subscribe(onNext: { (location) in
    print(location)
}).disposed(by: disposeBag)

目标:

  1. 如果速度属性仍然低于25 for 20 seconds,则打印位置
  2. 如果within 20秒的速度达到above 25,则取消发出的事件

当前问题是:如果速度低于25,并且在20秒之内可观察到的第二个事件的速度低于25,则由于debounce,它会取消上一个事件。

ios swift reactive-programming rx-swift
1个回答
0
投票

您应该添加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)
© www.soinside.com 2019 - 2024. All rights reserved.