If you call deleteBackwards
10000 times, this means that 10000 deleteBackwards commands have to be serialized into some representation that can be transmitted via XPC, sent over to the host process, deserialized, and then applied. This could be the source of your delay.
I would batch your deleteBackwards
calls into chunks that delete no more than the current document context you have available, then wait for the next textDidChange call, and if it is within X milliseconds of the last one, delete some more with the new context, as you can be fairly sure that the user didn't tap another text field so soon after pressing your button.
Below is some pseudocode that demonstrates what I mean.
var lastDeleteAllPressed: Double
let threshHold = 0.1 // you'll need to test on a real device to get a real value for this.
func textDidChange() {
//whatever else you need to do
if CACurrentMediaTime - lastDeleteAllPressed < threshHold && !proxy.isEmpty {
deleteKeyPressed()
}
}
func deleteKeyPressed() {
let length = proxy.contextBeforeInput.append(proxy.contextAfterInput).length
0...length.forEach { deleteBackwards() }
lastDeleteAllPressed = CACurrentMediaTime()
}
This approach has the benefit of not doing unnecessary work, and of working even if there are more than 10000 characters in the document the user is editing. The drawback is that it could fail if the host process takes unnaturally long to send back a textDidChange event.