Is there a simple way of implementing the Throttle feature in Reactive programming without having to use RxSwift or similar frameworks.
I have a textField delegate method that I would like not to fire every time a character is inserted/deleted.
How to do that using vanilla Foundation?
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
}
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
}
}