6.10 · Threads and run loops, observers, and what actually runs in a custom mode

The run loop questions you already know say what a loop is. These four show what it does when nobody drives it, when you watch it, and when you invent a mode.

Mental model. A run loop is a loop you must be running. On a thread you made it does nothing until you attach a source and call run; on the main thread it is the heartbeat that observers, timers and the main dispatch queue all hang from.

Loops you run yourself

Per-thread loops, observer activities and the main queue across modes.

  1. Why does a Timer created on a background thread never fire, and when does a thread’s run loop return immediately? 6.10.1 · L3
    Every thread can have a run loop, created lazily the first time you ask for RunLoop.current, but it is not running until you call run. A Timer only registers…
  2. What are run loop observers, in which order do they fire, and what do frameworks use them for? 6.10.2 · L5
    A CFRunLoopObserver is a callback attached to a loop for chosen activities: entry, before timers, before sources, before waiting, after waiting and exit. The…
  3. Which work still runs while the run loop is in a tracking or custom mode, and what does “common modes” really contain? 6.10.3 · L5
    Q217 explains why a default-mode Timer stalls during scrolling. The complete picture is a table of what is registered for which mode. The check runs the main…

More in iOS platform internals