Starting a loop is an abstraction that (among other things) provides a mechanism for processing input data sources (sockets, ports, files, keyboard, mouse, timers, etc.).
Each NSThread has its own run loop, which can be accessed through the currentRunLoop method.
In general, you do not need to directly access the startup loop, although there are some (network) components that can allow you to specify which loop loop they will use to process I / O.
The start cycle for this stream will wait for one or more of its input sources to have some data or an event, then run the appropriate input handler to process each input source that is βreadyβ.
After that, he will return to his cycle, process input from various sources and "sleep" if there is no work.
This is a pretty high level description (an attempt to avoid too many details).
EDIT
Trying to reply to a comment. I broke it into pieces.
- this means that I can only access / run to start the loop inside the thread right?
Really. NSRunLoop is not thread safe and should only be accessible from the context of the thread that starts the loop.
- Is there any simple example of how to add an event to a loop loop?
If you want to control the port, you simply add this port to the run loop, and then the run loop will watch this port for activity.
- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode
You can also add a timer using
- (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode
- What does it mean that it will return to the cycle?
The start of the loop will process all ready-made events at each iteration (in accordance with its mode). You will need to look at the documentation to find out about the launch modes, as this is a bit beyond the general answer.
- starts a loop inactive when a thread starts?
In most applications, the main startup loop starts automatically. However, you are responsible for starting the execution loop and responding to incoming events for the threads you rotate.
- Is it possible to add some events to the thread cycle loop outside the thread?
I'm not sure what you mean here. You do not add events to the run loop. You add input sources and timer sources (from the stream to which the trigger cycle belongs). Then the cycle loop tracks their activity. Of course, you can provide input from other threads and processes, but the input will be processed by a run loop that controls these sources in the thread that starts the run loop.
- Does this mean that sometimes I can use the run loop to block the thread for a while
Really. In fact, the loop will remain in the event handler until that event handler returns. You can see this in any application quite simply. Set a handler for any I / O action (for example, clicking a button) that will sleep. You will block the main run loop (and the entire user interface) until this method completes.
The same applies to any startup cycle.
I suggest you read the following documentation on startup loops:
https://developer.apple.com/documentation/foundation/nsrunloop
and how they are used in threads:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1