0

I need to indicate some state with delay, while user is typing. I'm trying to use RxSwift:

_textField.rx.text.orEmpty.debounce(3, scheduler: MainScheduler.instance).distinctUntilChanged().subscribe({[weak self] _ in
        self?.typeViewShould(hide: true)
    }).addDisposableTo(disposeBag)

The problem is after 3 seconds, I want to wait, block executes as many times as characters I entered, and not only once with latest value. I tried to rewrite code from GitHub example search, but it doesn't work:

        _ = _textField.rx.text.orEmpty.debounce(3, scheduler: MainScheduler.instance).distinctUntilChanged().flatMapLatest {[weak self] query -> Observable<[String]> in
        self?._textField.text = nil
        self?.typeViewShould(hide: true)
        return .just([])
    }.observeOn(MainScheduler.instance)

What am I doing wrong?

Alexandr Kolesnik
  • 1,929
  • 1
  • 17
  • 30

1 Answers1

0

'debounce' works as follows - http://rxmarbles.com/#debounce

if i understand your question correctly, you want something like this -

        let timer = Observable<Int>.timer(3.0, scheduler: scheduler)
        let textAfter3Seconds = Observable.combineLatest(timer, _textField.rx.text) { $1 }
Maxim Volgin
  • 3,957
  • 1
  • 23
  • 38