iel*_*ani 2 throttling reactive-programming ios swift
是否有一种简单的方法可以在反应式编程中实现 Throttle 功能,而无需使用 RxSwift 或类似框架。
我有一个 textField 委托方法,我不想在每次插入/删除字符时触发它。
如何使用香草粉底做到这一点?
Yes it is possible to achieve.
But first lets answer small question what is Throttling?
In software, a throttling process, or a throttling controller as it is sometimes called, is a process responsible for regulating the rate at which application processing is conducted, either statically or dynamically.
Example of the Throttling function in the Swift.
In case that you have describe with delegate method you will have issue that delegate method will be called each time. So I will write short example how it impossible to do in the case you describe.
class ViewController: UIViewController {
var timer: Timer?
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
@objc func validate() {
print("Validate is called")
}
}
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.validate), userInfo: nil, repeats: false);
return true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3693 次 |
| 最近记录: |