iOS Run Loop, What Why When

Prafulla Singh
4 min readFeb 18, 2020
Picked From Stack overflow in Credits

Run Loop is a mechanism that allows threads to process events at any time without exiting.

Run Loop is actually an object that manages the events and messages that it needs to process and provides an entry function to execute the logic of the Event.

After the thread executes this function, it will stay in the “accept message-> wait-> processing -> Sleep till next message -> accept message” loop inside the function until the end of the loop (such as a message passed to quit) and the function returns.

In OSX / iOS, We have NSRunLoop and CFRunLoopRef for same purpose. CFRunLoopRef is inside the CoreFoundation framework. It provides APIs for pure C functions, all of which are thread-safe.

NSRunLoop is a wrapper based on CFRunLoopRef that provides object-oriented APIs, but these APIs are not thread-safe.

The system has 5 modes registered by default:

  1. kCFRunLoopDefaultMode: App’s default mode, usually the main thread runs in this mode.
  2. UITrackingRunLoopMode: Interface tracking mode, used for ScrollView to track touch and slide, to ensure that the interface is not affected by other modes when sliding.
  3. UIInitializationRunLoopMode: The first mode that is entered when the app is started, it will not be used after the startup is…

--

--

Prafulla Singh
Prafulla Singh

No responses yet