RxSwift 限制如何只从流中获取最后一个值

RxSwift throttle how to get only last value from stream

我正在学习 RxSwift,我想要实现的是获得从 UITextFied 打印我 text 的机制,但是在给定的时间间隔之后。

现在如何工作:当我输入第一个字符时,这个字符会立即打印出来(而不是像我预期的那样延迟),如果我继续输入长句子,text 每两秒打印一次(因为间隔是在油门中设置的),但我只想拥有最新的 text 值。

我的代码:

inputField.rx.text.orEmpty
        .throttle(2, latest: true, scheduler: MainScheduler.instance)
        .subscribe(onNext: { text in
            print("\(text)")
        }, onDisposed: nil)
        .addDisposableTo(disposeBag)

我正在寻找你的帮助 Rx 研究员 :) 谢谢

使用 debounce 而不是 throttle

来自 debounce 的文档:

Ignores elements from an observable sequence which are followed by another element within a specified relative time duration, using the specified scheduler to run throttling timers.

来自 throttle 的文档。

Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.