Runloop
Runloop is an implementation of the event receiving and distribution mechanism. Is part of the thread-specific infrastructure. A Runloop is an event processing loop that continuously dispatches work and processes input events. The purpose of using runloop is to let your thread
The main purpose of RunLoop:
If there is no RunLoop, UIApplicationMain will return directly after execution. That is, the program is started and then finished. It is busy when there is work and dormant when there is no work.
When to use Runloop?
Runloop is used only when you need to interact with the thread.
Runloop Mode
A Runloop may have several modes
A Runloop Mode is actually a collection of sources, timers, and observers. Different modes isolate different groups of sources, timers, and observers. Runloop can only run in one Mode at a time, handling the Source, Timer, and Observer in that Mode.
There are five modes mentioned in apple documentation, which are:
NSDefaultRunLoopMode: default mode, normally in this mode
NSConnectionReplyMode
NSModalPanelRunLoopMode
NSEventTrackingRunLoopMode: use this Mode to track events from the user interaction (such as a UITableView slide up and down)
NSRunLoopCommonModes
The only publicly exposed modes in iOS are NSDefaultRunLoopMode and NSRunLoopCommonModes. NSRunLoopCommonModes is actually a collection of the Mode, including NSDefaultRunLoopMode and NSEventTrackingRunLoopMode by default.
Source
Events that wake up the Runloop. For example, when the user clicks on the screen, an input source is created.
- Source0: non-system event
- Source1: indicates the system time
The NSTimer that we use a lot falls into this category.
Observer An Observer can listen for status changes of the Runloop and react to them.
RunLoop Runs the process
When nothing is going on, the Runloop goes to sleep. When the external source wakes it up, it processes the received timer/source in turn, and then goes to sleep again.
Read the original