DevPrep

All topics › Multithreading & concurrency

Run loop

Middle PRO

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:

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store