Suggested approach: This lets you attach code to the user interface drawing loop so that your code always gets called immediately after a frame has been drawn and you have maximum time available to you.
For bonus points, try to compare it to a more naive solution such as
Estimated difficulty: Advanced
UserDefaultsgood for? What is
UserDefaultsnot good for?
Link copied to your pasteboard.