What is RunLoop?


I would like to know what the RunLoop in iOS is and how it interacts with it.

It would also be interesting to understand how these two concepts interact with the autorelease pool.

asked by anonymous 23.12.2013 / 13:05

1 answer


Every thread has a run loop (instance of NSRunLoop ), which is responsible for handling events such as a touch screen or running a scheduled method with NSTimer .

According to the Threading Programming Guide :


A run loop is an event-processing loop that you use to schedule tasks and coordinate incoming event reception. The purpose of a run loop is to keep your thread busy when there are tasks to accomplish and put the thread to sleep when there is none. >

In general, you should run the run loop associated with the thread , if you wish. In the case of the main thread , responsible for handling events related to the user interface, the system will execute the run loop .

At each run loop iteration, an autorelease pool is created at the beginning, which is drained at the end of the iteration, after handling an event. This ensures that objects created during the handling of an event will not be deallocated until the end of the treatment when the run loop iteration ends.

I hope I've come up with a general idea about run loops , enough for most practical uses. If you want to go deeper, I recommend reading the Threading Programming Guide , which deals with ports, modes, and other concepts associated with run loops .

23.12.2013 / 18:18