NSScrollView with NSTextView calls drawRect too many times

113 Views Asked by At

In a NSDocument-based application, each text document is displayed in a NSTextView enclosed in a NSClipView which is itself enclosed in a NSScrollView.

• case 1: When I load a text whose height is smaller than the scroll view frame, after the text is displayed, the drawRect method of the NSTextView keeps being called a few times per second, even when nothing happens on screen and when the keyboard or mouse is not used. But In Xcode the displayed CPU usage returns to 0% after a few seconds and energy use is negligible.

• case 2: When the loaded text is too big to be entirely displayed in the frame, the scrollview's drawRect method is called dozens of times per second and CPU usage stabilises about 80 to 90% (with high energy impact) even after several minutes with no user interaction with the computer and the application!

Then with this long text, once the user has scrolled down to the end of it, the behaviour returns to the first case (no CPU use, negligible Energy impact, a few drawRect calls per second), whatever the cursor position is.

As I understood that drawRect is called only when some changes occurred in the view or its subviews, I wonder why the calls are so numerous when the application is in standby, with such an impact on CPU Use. I profiled in Instruments, and the CPU usage is very high with more than 99% of it associated to the drawRect method.

This is both very intriguing and a serious problem of performance for my application. I precise that I never call any drawRect method myself (nor use setNeedsDisplay).

The App is developed with Xcode 13.2.1 running on Monterey 12.0.1 - and compiled after 10.14 Kit

0

There are 0 best solutions below