Run loop
The essence
A RunLoop is an event-processing loop bound to a thread: it waits for an event, handles it and goes back to sleep without burning CPU. Each thread can have exactly one run loop, but it is not created automatically: the object appears lazily on the first access to RunLoop.current, and it only spins if you explicitly start it (run()). The exception is the main thread: UIApplicationMain starts its run loop for you, and that loop handles touch events, timers, redrawing and DispatchQueue.main callbacks.
Event sources:
- Input sources — asynchronous events: ports,
perform(_:on:), system calls. - Timer sources —
TimerandCADisplayLink, fired on schedule, but only while the run loop is in the right mode.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep