【问题标题】:Problems using RxSwift使用 RxSwift 的问题
【发布时间】:2017-08-19 14:36:33
【问题描述】:

当用户键入时,我需要指示一些延迟状态。我正在尝试使用 RxSwift:

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

问题是 3 秒后,我想等待,块执行的次数与我输入的字符一样多,而不仅仅是一次具有最新值。我试图从 GitHub 示例搜索中重写代码,但它不起作用:

        _ = _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)

我做错了什么?

【问题讨论】:

    标签: swift rx-swift rx-cocoa


    【解决方案1】:

    “去抖动”的工作原理如下 - http://rxmarbles.com/#debounce

    如果我正确理解你的问题,你想要这样的东西 -

            let timer = Observable<Int>.timer(3.0, scheduler: scheduler)
            let textAfter3Seconds = Observable.combineLatest(timer, _textField.rx.text) { $1 }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多