簡體   English   中英

如何觀察屬性在 RxSwift 中的特定時間間隔內是否沒有變化

[英]How to observe if a property doesn't change for a specific interval in RxSwift

我想觀察 CLLocation 更新的speed屬性,如果速度在 10 秒內沒有改變並且速度改變重置計時器,則觸發事件。 到目前為止只能想出這么多代碼。

let location: Observable<CLLocation>

location.subscribe(onNext: { (coordinates) in
    print(coordinates)
})
.disposed(by: disposeBag)

我想我們可以使用debouncethrottle但不確定如何使用。

嘗試這個:

// 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM