ios - 由 EventProducerType 生成的节流事件 - Swift Bond

标签 ios swift

我想限制 SwiftBondEventProducerType 生成的事件框架。

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

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/

相关文章:

ios - 如何增加 uiswitch Thumb 大小

swift - 如何快速组织 View Controller 之间共享的数据

ios - Swift 项目链接器错误

iphone - NSRunLoop 和 NSAutoreleasePool,它们是如何交互的?

ios - 无法从 Jenkins 运行脚本

ios - 在 Xcode 上归档时按顺序使用 max() 和 min() 的 Swift 问题

swift - 探路者 SWIFT

ios - SpriteKit : sprite looks blurry (with ghosting) at high velocity but fine at low velocity

objective-c - NSLog 的输出到底是什么意思?

ios - 将 iOS 应用程序限制为只能安装在一台设备上