Menu

Two SwiftUI animation loops that burned CPU on my Mac with nothing on screen
📰

Two SwiftUI animation loops that burned CPU on my Mac with nothing on screen

DEV Community: swift·Apps by Mo·5 days ago
#zJUEMavc
#devtoswift#devto#dev#signup#animation
Reading 0:00
15s threshold

A looping animation looks free. On macOS, two of mine were not. Both shipped in my own apps, and both were measured on release builds, not Debug.

1. A glow that kept running after its popover closed

Soluna is my menu bar calendar. Today’s cell had a slow “breathing” glow, a .repeatForever animation on one flag. My own Mac health app, What Happened, reported Soluna using about 134 core-minutes a day in the background.

The cause: AppKit keeps an NSPopover ’s hosting view alive between opens. So after the popover closed, SwiftUI went on re-rendering its whole view tree every frame. sample showed the main thread in NSHostingView.layout and ViewGraphRootValueUpdater.render with nothing on screen.

I proved it by toggling. Open and close the popover: 17 to 19% of a core, and it stays. Close it on a month that does not contain today’s cell: 0.0%. Back to today: 17% again.

The rule I took from it: any looping animation inside a popover or panel has to stop when it closes.

2.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.