我想限制 SwiftBond 的 EventProducerType
生成的事件框架。
以下是我测试如何限制事件的方法:
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)
}
通过这种方法,没有限制,并且文本更改后会立即打印。我的目标是限制 UISearchBar 的搜索文本,以发出网络请求以从端点检索一些数据。
最佳答案
我让它可以与以下内容一起使用:
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)
}
关于ios - 由 EventProducerType 生成的节流事件 - Swift Bond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36229783/