What is the Runloop?

Asked

Viewed 239 times

7

I’d like to know what Runloop is in iOS and how it and the corresponding thread interact.

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

1 answer

9

All thread has a loop run (instance of NSRunLoop), who is responsible for handling events, such as a touch of the screen or the execution of a method scheduled with NSTimer.

According to the Threading Programming Guide:

A loop run is an event processing loop that you use to schedule tasks and coordinate the reception of incoming events. The purpose of an loop run is to keep your thread busy when there are tasks to perform and place the thread to sleep when there is no.

In general you are the one who must perform the loop run associated with thread, if desired. In the case of thread responsible for handling events related to the user interface, the system is in charge of executing the loop run.

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

I hope I have a general idea about run loops, sufficient for most practical uses. If you want to delve deeper, I recommend reading the Threading Programming Guide, dealing with doors, modes and other concepts associated with run loops.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.