从EventProducerType - Swift Bond产生的Throttle事件

时间:2016-03-25 23:35:03

标签: ios swift

我希望限制EventProducerType SwiftBond框架var throttledObserver: EventProducer<String?>! init() { throttledObserver = Observable<String?>(nil).throttle(1000, queue: Queue.Main) throttledObserver.observeNew { text in // This is always printed no matter how large the throttle time interval is print(text) } } // UISearchBarDelegate method func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { throttledObserver.next(searchText) } 产生的事件。

以下是我如何测试如何限制事件:

{{1}}

使用这种方法,没有限制,文本在文本更改时立即打印。我的目标是限制UISearchBar的搜索文本,以使网络请求从端点检索一些数据。

1 个答案:

答案 0 :(得分:0)

我得到了以下内容:

var throttledObserver = Observable<String?>!

init() {
  throttledObserver = Observable<String?>(nil)

  // the key is not to save the returned value from throttle into a variable, but to set the observation immediately
  throttledObserver.throttle(1000, queue: Queue.Main)
    .observeNew { text in
      print(text)
    }
}

// UISearchBarDelegate method
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
  throttledObserver.next(searchText)
}