controlling NSTextView updates to deal with flicker and improve speed
-
26-09-2019 - |
Question
I have a subclassed NSTextView that I am manipulating in a separate thread (using performSelectorOnMainThread:withObject:waitUntilDone:) using selectors replaceCharactersInRange:withString:, setSelectedRange:, and insertText:. I'm experiencing flickering of text, and poor performance as the NSTextView updates the display for each and every operation.
Any suggestions on how to control when the display is updated, so I can update it only when actually needed? I tried using various combinations setNeedsDisplay:NO (from both the main and background threads, before and after my updates) which seems to be ignored.
Thanks in advance to anyone who can provide some insight.
Solution
I think you should be manipulating the underlying NSTextStorage
for the text view, rather than invoking the view's event-related methods directly. This is a pretty classic example of a Model-View-Controller architecture: the NSTextView
is the view and the NSTextStorage
is the model. Whenever possible, you want to manipulate the model directly and let the controller/view layers deal with updating the view as they see fit.